React re-render 5 - context
2022年5月1日小于 1 分钟
React re-render 5 - context
Question
What does the code snippet to the right output by console.log
?
Snippet
import React, { useState, memo, createContext, useEffect, useContext} from 'react'
import ReactDOM from 'react-dom'
const MyContext = createContext(0);
function B() {
const count = useContext(MyContext)
console.log('B')
return null
}
const A = memo(() => {
console.log('A')
return <B/>
})
function C() {
console.log('C')
return null
}
function App() {
const [state, setState] = useState(0)
useEffect(() => {
setState(state => state + 1)
}, [])
console.log('App')
return <MyContext.Provider value={state}>
<A/>
<C/>
</MyContext.Provider>
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App/>)
Answer
"App"
"A"
"B"
"C"
"App"
"B"
"C";