React 生命周期函数执行顺序

本文详细阐述了React组件在数据加载、数据更新和组件销毁时的生命周期方法执行顺序。在16.4版本后,getDerivedStateFromProps取代了componentWillMount,用于在组件接收到新属性时更新state。数据更新时,执行顺序包括shouldComponentUpdate、componentWillUpdate、render和componentDidUpdate,其中getSnapshotBeforeUpdate与componentDidUpdate配合使用。在错误处理中,getDerivedStateFromError和componentDidCatch起到了关键作用。
摘要由CSDN通过智能技术生成

目录

一、数据加载时

二、数据更新时

三、组件销毁


一、数据加载时

constructor-->componentWilMount-->render-->componentDidMount

注:在版本16.4之后,废除了componentWillMount,则顺序如下:

constructor()-->static getDerivedStateFromProps()-->render()-->componentDidMount()

import React, { Component } from 'react'

export default class Lifecycle extends Component {
    constructor(props){
        super(props);
        console.log('01 构造函数');
    }
    //组件将要被挂载
    componentWillMount(){
        console.log('02 组件挂载前触发');
    }
    //组件挂载完成
    componentDidMount(){
        console.log('04 组件挂载完成');
    }
   
  render() {
      console.log('03 数据渲染');
    return (
      <div>
         <h2>React 声明周期函数</h2>
      </div>
    )
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值