这次趁着学习React过一下生命周期,主要是参考了官网生命周期的内容然后加了些自己的理解,做个学习记录,后续可能会继续完善相关内容
1.概览
React 16:
- 页面初始化:constructor–>componentWillMount–>render–>componentDidMount
- 父级数据更新:componentWillReceiveProps–>shouldComponentUpdate–>componentWillUpdate–>render–>componentDidUpdate
- 组件数据更新:shouldComponentUpdate–>componentWillUpdate–>render–>componentDidUpdate
- 强制更新:componentWillUpdate–>render–>componentDidUpdate
2.特点:(待完善)
- 绿色:页面加载后只走一次
- 蓝紫色:父级数据更新会引发render
- 黄紫色:子组件更新数据会引起render
- 黑紫色:强制更新会跳过SCU,引发render
3.生命周期方法:(待完善)
生命周期函数 | 函数参数及功能 |
---|---|
render() | 常用,唯一必须的生命周期 |
constructor(props) | 常用,非必须,可以不用(不初始化状态和方法时) |
componentDidMount() | 常用,请求接口,挂载方法和定时器 |
componentDidUpdate(prevProps,prevState,shapshot) | 常用,组件更新后调用,按需请求接口 |
componentWillUnmount | 常用,卸载方法和销毁定时器 |
componentWillUpd |