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