react 18.0.0 版本生命周期

react18版本生命周期
组件挂载时
  • constructor
  • static getDerivedStateFromProps(纯函数,静态方法)
  • render
  • componentDidMount
数据更新时
  • static getDerivedStateFromProps
  • shouldComponentUpdate
  • render
  • getSnapshotBeforeUpdate
  • componentDidUpdate
组件卸载时
  • componentWillUnmount
捕获父子组件异常时
  • componentDidCatch
新增生命周期
  • static getDerivedStateFromProps
  • getSnapshotBeforeUpdate
从v16.3开始弃 componentWillMount、componentWillReceiveProps 、componentWillUpdate 三个钩子函数
import React, { Component } from 'react'

export default class index extends Component {
    constructor(props) {
        console.log("constructor============1");
        super();
        this.state = {
            msg: '这是更新前'
        }
    }
    componentDidMount() {
        console.log("componentDidMount=========================3");
    }
    componentDidUpdate() {
        console.log('更新时');
    }
    componentWillUnmount() {
        console.log('卸载时');
    }
    componentDidCatch() {
        console.log("捕捉到异常了");
    }
    static getDerivedStateFromProps(nextProps, prevState) {
        console.log(nextProps, prevState, "纯函数,静态方法");
        return null
    }
    getSnapshotBeforeUpdate(prevProps, prevState) {
        console.log(prevProps, prevState, "需要搭配 componentDidUpdate来使用");
        return null
    }
    shouldComponentUpdate() {
        console.log('是否更新当前组件数据');
        return false
    }
    setDate = () => {
        this.setState({
            msg: '这是更新后'
        })
    }
    render() {
        console.log("render====================2");
        return (
            <div>
                <button onClick={this.setDate}>{this.state.msg}</button>
            </div>
        )
    }
}


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值