React生命周期函数以及使用场景

1、挂载阶段:

  defaultProps和propsTypes -> constructor和this.state里面数据的初始化 -> componentWillMount -> render -> componentDidMount(组件挂载完毕)

2、数据更新:

  shouldComponentUpdate(nextProps, nextState)(接下来props的样子,接下来state的样子)确认是否要更新数据,返回 true 表示更新 -> componentWillUpdate -> render -> componentDidUpdate

3、props改变:

  父组件传值给子组件导致props的值发生改变的时候 componmentWillReceiveProps(nextProps, nextState)

4、组件销毁:

  componentWillUnmount

5、组件发生错误:

  componentDidCatch()

componentWillMount:

componentDidMount:可以使用 this.state的, 这个生命周期函数只会在挂载执行一次,可以进行异步请求数据

componmentWillReceiveProps(nextProps, nextState):父组件传递的属性有变化,做相应的响应

shouldComponentUpdate(nextProps, nextState):组件是否需要更新,需要返回bool类型的结果,也是个优化点

componentWillUpdate:组件将要更新,可做更新统计

componentDidUpdate:组件更新完毕

componentWillUnmount:组件卸载,可做清理工作

 

posted on 2020-09-03 11:40  棋士牧生  阅读(275)  评论(0)    收藏  举报

导航