react组件生命周期

1. Mounting/组建挂载相关

  (1)componentWillMount

    组件将要挂载。在render之前执行,但仅执行一次,即使多次重复渲染该组件或者改变了组件的state

  (2)componentDidMount

    组件已经挂载。在render之后执行,同一个组件重复渲染只执行一次。

2. Updating/组建更新相关

  (1)componentWillReceiveProps(object nextProps)

    已加载组件收到新的props之前调用,注意组件初始化渲染时则不会执行

  (2)shouldComponentUpdate(object nextProps, object nextState)

    组件判断是否重新渲染时调用。该接口实际是在组件接收到了新的props或者新的state的时候会立即调用

  (3)componentWillUpdate(object nextProps, object nextState)

    组件将要更新

  (4)componentDidUpdate(object nextProps, object nextState)

    组建已经更新

3. Unmounting/组件移除相关

  (1)componentWillUnmount

    在组件要被移除之前的时间点触发,可以利用该方法来执行一些必要的清理

4. 生命周期中与props和state相关

  (1)getDefaultProps  设置props属性默认值

  (2)getInitialState      设置state属性初始值  

猜你喜欢

转载自www.cnblogs.com/ImaY/p/9037383.html