React的生命周期

158 阅读1分钟

React生命周期

React16之后将要废弃生命周期

  • componentWillMount
  • componentWillReceiveProps
  • componentWillUpdate 保留了UNSAFE_前缀的三个函数,为了向下兼容。应避免使用

挂载阶段

  1. constructor:构造函数,通常在构造函数中初始化state对象或者给自定义方法绑定this
  2. getDerivedStateFromProps:将要
  3. render
  4. componentDidMount:组件装载之后调用,可以获取到DOM节点并操作。

更新阶段

  1. getDerivedStateFromProps
  2. shouldComponentUpdate
  3. render
  4. getSnapshotBeforeUpdate
  5. componentDidUpdate

卸载阶段

  1. componentWillUnmount