React生命周期, setState、props改变触发的钩子函数

449 阅读1分钟

一. setState改变会触发的生命周期钩子

  1. shouldComponentUpdate
  2. componentWillUpdate
  3. render
  4. componentDidUpdate

二. props改变会触发的生命周期

  1. componentWillReveiceProps
  2. shouldComponentUpdate
  3. componentWillUpdate
  4. render
  5. componentDidUpdate

三.componentDidUpdate的使用

componentDidUpdate(prevProps,prevState){
  if(prevProps!=this.props){
    
  }
}

例如:从外部传进来的dataSource这个变量

componentDidUpdate(prevProps,prevState){
    if(prevProps.dataSource!=this.props.dataSource){
     ///这里处理something
    }
}

四.注意

无意义使用:componentWillMount,componentWillUnmount;

有条件使用:componentDidUpdate;

禁止使用:componentWillUpdate,shouldComponentUpdate;

正常使用:componentWIllReceiveProps,componentDidMount。 componentWillMount和componentWillReceiveProps中,setState会被react内部处理,而不触发render;其他生命周期均正常出发更新渲染。