- 首次实力化
- getDefaultProps
- getInitialState
- componentWillMount
- render
- componentDidMount
- 实力化完成后的更新
- getInitialState
- componentWillMount
- render
- componentDidMount
- 组建已存在时的状态改变
- componentWillReceiveProps
- shouldComponentUpdate
- componentWillUpdate
- render
- componentDidUpdate
- componentWillUnmount
- 生命周期共提供了10个不同的api
-
getDefaultProps
作用于组件类,只调用一次,返回对象用于设置默认的props,对于引用值,会在实例中共享 -
getInitialState
作用于组件的实例,在实例创建时调用一次,用于处事话每个实例的state,此时可以访问this.props -
componentWillMount
在完成首次渲染之前调用,此时仍可以修改组件的state -
render
必选的方法,创建虚拟dom,该方法具有特殊的规则:只能通过this.props和this.state访问数据
可以返回null,false或任何react组件
只能出现一个顶级组件
不能改变组件的状态
不能修改dom的输出 -
componentDidMount
真实的dom被渲染出来后调用,在该方法中可通过this.getDOMNode()访问到真实的dom元素。此时已可以使用其他类库来操作这个dom
在服务端中,该方法不会被调用.
-
componentWillReceiveProps 组件接受到新的props时调用,并将其作为参数nextProps使用,此时可以更改组件props及state.
componentWillReceiveProps:function(nextProps){
if(nextProps.bool){
this.setState({
bool:true
})
}
}
- shouldComponentUpdate
组件是否应当渲染新的props或state,返回false表示跳过后续的生命周期方法,
通常不要使用一面出现bug.在出现应用的瓶颈时,可以通过该方法进行适当的优化.
在首次渲染期间或者调用了forceUpdate方法后,该方法不会被调用
-
componentWillUpdate
接受到新的props或者state后,进行渲染之前调用,此时不允许更新props或state. -
componentDidUpdate
完成渲染新的props或者state后调用,此时可以访问到新的dom元素 -
componentWillUnmount
组件被移除之前被调用,可以用于做一些清理工作,在componentDidMount方法中添加的多有任务都需要在该方法中撤销,
比如创建的定时器货添加的时间监听器。