一. setState改变会触发的生命周期钩子
- shouldComponentUpdate
- componentWillUpdate
- render
- componentDidUpdate
二. props改变会触发的生命周期
- componentWillReveiceProps
- shouldComponentUpdate
- componentWillUpdate
- render
- 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;其他生命周期均正常出发更新渲染。