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