React组件不同状态下生命周期执行顺序

  1. 初次挂载: getDefaultPropsgetInitialStatecomponentWillMountrendercomponentDidMount
  2. 卸载:componentWillUnmount
  3. 重新挂载: getInitialStatecomponentWillMountrendercomponentDidMount
  4. 组件更新:componentWillReceivePropsshouldCompnentUpdatecomponentWillUpdatecomponentDidUpdate

为什么重新挂载的时候没有getDefaultProps,因为React的生命周期主要通过3个阶段进行管理:MOUNTING,RECEIVE_PROPS和UNMOUNTING,3个阶段分别对应3个方法:mountComponent,updateComponent和ummountComponent;其中mountComponent负责管理生命周期中的getInitialStatecomponentWillMountrendercomponentDidMount,由于getDefaultProps是通过构造函数进行管理,也是整个生命周期中最先开始执行的,而mountComponent无法调用到getDefaultProps,所以为什么getDefaultProps只执行一次的原因。

原文:大专栏  React组件不同状态下生命周期执行顺序


猜你喜欢

转载自www.cnblogs.com/petewell/p/11607326.html