react新的生命周期

一. react16当前生命周期

  • componentWillMount
    render前,所以setState时不会重新渲染,服务端渲染唯一调用,推荐用constructor代替之
  • render
  • componentDidMount
    render后,调用setState会重新渲染,页面可交互,可以请求数据
  • componentWillRecieveProps(nextProps)
    已挂载组件接收到新props触发
  • shouldComponentUpdate(nextProps, nextState)
    在接收到新的props或state时是否重新渲染,默认返回true;首次渲染或forceUpdate时不会触发;
  • componentWillUpdate(nextProps, nextState)
    如果shouldComponentUpdate返回false,update相关的函数都不会触发;不要使用setState;
  • componentDidUpdate(nextProps, nextState)
  • componentWillUnmount
    卸载组件

二. 由于未来采用异步渲染机制,所以即将在17版本中去掉的生命周期钩子函数

  • componentWillMount
  • componentWillRecieveProps
  • componentWIllUpdate

三. 新增两个

  • static getDerivedStateFromProps
    实例化组件之后及重新渲染组件之前触发,类似于componentWillReceiveProps,可以用来控制 props 更新 state 的过程;它返回一个对象表示新的 state;如果不需要更新,返回 null 即可
  • getSnapshotBeforeUpdate
    render之后,组件重新渲染之前,对视图做出实际改动前被调用,返回值将作为 componentDidUpdate 的第三个参数

猜你喜欢

转载自www.cnblogs.com/colima/p/9484607.html