APP.js
import React, { Component } from 'react'
function CompB() {
return (
<div>
<h1>标题</h1>
{['abc', null]}
</div>
)
}
class CompA extends Component {
state = {}
constructor() {
super()
console.log('4', 'CompA constructor')
}
static getDerivedStateFromProps(props, state) {
console.log('5', 'CompA getDerivedStateFromProps')
return null
}
componentDidMount() {
console.log('7', 'CompA componentDidMount')
}
componentWillUnmount() {
console.log('9', "CompA componentWillUnmount")
}
render() {
console.log('6', 'CompA render')
return (
<div>
<CompB />
</div>
)
}
}
export default class App extends Component {
state = {}
constructor() {
super()
console.log('1', 'App constructor')
}
static getDerivedStateFromProps(props, state) {
console.log('2', 'App getDerivedStateFromProps')
return null
}
componentDidMount() {
console.log('8', 'App componentDidMount')
}
componentWillUnmount() {
console.log('10', "App componentWillUnmount")
}
render() {
console.log('3', 'App render')
return (
<div>
<CompA />
</div>
)
}
}
由于react现在严格模式(React.StrictMode)下组件会运行两次 所以运行结果为 112233445566789(10)78
去掉严格模式运行一次就是 12345678 我给排好续了,按照渲染流程应该不难理解