父组件创建及创建的时候,如果有子组件,子组件会经历哪些生命周期阶段,顺序是怎样的?

当父组件创建时,子组件将依次经历constructor、getDerivedStateFromProps、render、componentDidMount等生命周期阶段。而在父组件更新时,子组件会执行gerDerivedStateFromProps、shouldComponentUpdate、render、getSnapshotBeforeUpdate和componentDidUpdate。详细过程包括代码演示和执行结果展示。
摘要由CSDN通过智能技术生成

1 、父组件创建的时候,父、子组件经历的周期及顺序如下所示

  1. 执行App组件中的constructor;
  2. 执行App组件中的getDerivedStateFromProps
  3. 执行App 组件中 Render函数
  4. 执行子组件组件的constructor
  5. 执行子组件中的getDerivedStateFromProps"
  6. 执行子组件中的render函数
  7. 执行子组件中的执行componentDidMount
  8. 执行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() 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值