React生命周期的变化

1、ES6语法的引入,砍掉了getDefaultProps和getInitialState

getDefaultProps 使用 static default={}的方式代替
getInitialState 使用 state属性替代,初始化可以写在constructor里,也可以写成类属性

2、下一代React版本,生命周期又有改变 

componentWillMount – 使用constructor或componentDidMount代替
componentWillUpdate – 使用componentDidUpdate代替
componentWillReceiveProps – 使用一个新的方法:static getDerivedStateFromProps来代替

React 16.3版本中:

componentWillMount,componentWillUpdate,componentWillReceiveProps还能用

React 16.x版本中:

启用弃用警告,三个方法变为:

UNSAFE_componentWillMount
UNSAFE_componentWillReceiveProps
UNSAFE_componentWillUpdate

在React17.0版本中:

会移除这三个周期。

详见https://github.com/facebook/react/issues/12152

3. static getDerivedStateFromProps

在下列三种情况下,会调用getDerivedStateFromProps方法:

1. 组件实例化。
2. 组件的props发生变化。
3. 父组件重新渲染。

this.setState()不会触发getDerivedStateFromProps(),但是this.forceUpdate()会。
在update阶段也会调用一次这个方法。

posted @   全玉  阅读(402)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示