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阶段也会调用一次这个方法。
最新文章
- Linux系统编程:基本I/O系统调用
- 记录vmware虚拟机安装的时候一些注意
- HDU 1698 Just a Hook(线段树/区间更新)
- 在没装VS2010的机器上运行VS2010开发的C++程序
- springMVC搭建
- 第三十三章 metrics(1) - graphite搭建 + whisper存储模式 + 高精度向低精度聚合方式 + 集成StatsD + 集成grafana
- RecyclerView (一) 基础知识
- Android中xml设置Animation动画效果详解
- JS和C# 互相调用与数据绑定问题
- 慕课网-安卓工程师初养成-2-10 Java中的强制类型转换
- 基于nodejs的消息中心
- java中的Package语句和import语句
- Linux智能小开关rfkill
- 权限的分类(shiro项目中来的五)
- 对国内IoT的展望
- Spring Boot 的 application.properties
- 国庆JAVA作业
- 【LeetCode每天一题】Pow(x, n)(平方)
- 配置Hanlp自然语言处理进阶
- (转)android系统架构及源码目录结构