React生命周期总结(新旧版本都有),React即将废弃的勾子

本文概述了React 16到17版本生命周期方法的变化,重点介绍了新版的getDerivedStateFromProps和getSnapShotBeforeUpdate,以及旧版中即将废弃的will*钩子,提供了一个清晰的迁移路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

旧版生命周期(16版本)

在这里插入图片描述

初始化阶段:由ReactDOM.render()触发 – 初次渲染
  1. constructor()
  2. componentWillMount()「新版本不推荐使用」
  3. render()
  4. componentDidMount()
更新阶段:由组件内部this.setState() 或父组件重新render触发
  1. shouldComponentUpdate()
  2. componentWillUpdate()「新版本不推荐使用」
  3. render()
  4. componentDidUpdate()
卸载组件阶段:由ReactDOM.unmountComponentAtNode() 触发
  1. componentWillUnmount()

新版生命周期(17版本)

在这里插入图片描述

初始化阶段:由ReactDOM.render()触发 – 初次渲染
  1. constructor()
  2. getDerivedStateFromProps()
  3. render()
  4. componentDidMount()
更新阶段:由组件内部this.setState() 或父组件重新render触发
  1. getDerivedStateFromProps()
  2. shouldComponentUpdate()
  3. render()
  4. getSnapShotBeforeUpdate()
  5. componentDidUpdate()
卸载组件阶段:由ReactDOM.unmountComponentAtNode() 触发
  1. componentWillUnmount()

即将废弃的勾子

  1. componentWillUpdate()
  2. componentWillReceiveProps()
  3. componentWillMount()
    记法:除了componentDidUpdate()这个组件卸载阶段的勾子外,其他带Will的勾子在新版本中都不推荐使用

⚠️ componentWillUpdate()、componentWillReceiveProps()、componentWillMount()在react新版本中需要加上UNSAFE_前缀才可以使用,否则会出现警告,而且在未来的版本中,这3个勾子可能被废弃。

来源:
尚硅谷React教程,这个课很好

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值