1 、父组件创建的时候,父、子组件经历的周期及顺序如下所示
- 执行App组件中的constructor;
- 执行App组件中的getDerivedStateFromProps
- 执行App 组件中 Render函数
- 执行子组件组件的constructor
- 执行子组件中的getDerivedStateFromProps"
- 执行子组件中的render函数
- 执行子组件中的执行componentDidMount
- 执行App组件中 componentDidMount
代码演示
import React, {
Component } from "react";
/*
父组件创建的时候,如果有子组件,子组件会经历哪些生命周期阶段,顺序是怎样的?
*/
//Demo中为子组件
class Demo extends Component {
constructor(props) {
super(props);
console.log("---1. 执行 Demo组件的constructor");
}
state = {
};
static getDerivedStateFromProps(nextProps, nextState) {
console.log("---2 执行getDerivedStateFromProps");
return null;
}
render() {
console.log("---3 执行render函数");
return <h1>Demo 组件</h1>;
}
componentDidMount()