Skip to main content

React 有哪些生命周期钩子函数?

组件的生命周期可分成三个状态:

  1. Mounting(挂载):已插入真实 DOM
  2. Updating(更新):正在被重新渲染
  3. Unmounting(卸载):已移出真实 DOM

React.Component - React

React Lifecycle Methods diagram

挂载

  1. constructor : 在 React 组件挂载之前,会调用它的构造函数。

  2. getDerivedStateFromProps:在调用 render 方法之前调用,并且在初始挂载及后续更新时都会被调用。

  3. render:render() 方法是 class 组件中唯一必须实现的方法。

  4. componendDidMount(): a. 在组件挂载后(插入 DOM 树中)立即调用。 b. 请求放在一般 componentDidMount 里,组件已经生成了对应的 DOM 结构,可以通过 this.getDOMNode()来进行访问。

render() 方法是 class 组件中唯一必须实现的方法,其他方法可以根据自己的需要来实现。

更新

每当组件的 state 或 props 发生变化时,组件就会更新。

  1. getDerivedStateFromProps():在调用 render 方法之前调用,并且在初始挂载及后续更新时都会被调用。根据 shouldComponentUpdate() 的返回值,判断 React 组件的输出是否受当前 state 或 props 更改的影响。

  2. shouldComponentUpdate():当 props 或 state 发生变化时,shouldComponentUpdate() 会在渲染执行之前被调用。

  3. render(): render() 方法是 class 组件中唯一必须实现的方法。

  4. getSnapshotBeforeUpdate():在最近一次渲染输出(提交到 DOM 节点)之前调用。

  5. componentDidUpdate(): 在更新后会被立即调用。

卸载

当组件从 DOM 中移除时会调用如下方法:

componentWillUnmount():在组件卸载及销毁之前直接调用。