首页 > 技术文章 > React Native生命周期

haojf 2018-10-11 10:15 原文

一、装载阶段

是组件的绘制阶段,完成了组件的加载和初始化。

getDefaultProps

在组建创建之前,会先调用getDefaultProps(),全局调用一次,严格说这不是组件生命周期的一部分。在组建被创建并且加载时,首先调用getInitialState(),来初始化组件的状态。

componentWillMount

然后,准备加载组件,会调用componentWillMount().

这个函数调用时机是在组件创建,并初始化状态后,在第一次绘制render()z之前。可以在这里做一些业务初始化操作,也可以设置组件状态。整个生命周期中只调用一次。

componentDidMount

在组件第一次绘制之后,会调用componentDidMount(),通知组件以及加载完成。

二、更新阶段

componentWillReceiveProps

如果组件收到新的属性(props),就会调用componentWillReceiveProps().

shouldComponentUpdate

当组件接受到新的属性改变的话,都会触发shouldComponentUpdate(),返回boolen

componentWillUpdate

当组件的状态或者属性改变,并且shouldComponentUpdate()返回true,就开始准备更新组件,并且调用componentWillUpdate.

componentDidUpdate

调用render()更新完成界面后,会调用componentDidUpdate()来得到通知。

 

三、卸载阶段

componentWillUnmont

当组件要被从界面上移除时,调用componentWillUnmount()

生命周期调用次数能否使用 setSate()
getDefaultProps 1(全局调用一次)
getInitialState 1
componentWillMount 1
render >=1
componentDidMount 1
componentWillReceiveProps >=0
shouldComponentUpdate >=0
componentWillUpdate >=0
componentDidUpdate >=0
componentWillUnmount 1
 

Race604

推荐阅读