组件的生命周期可分成三个状态:
Mounting(挂载):已插入真实 DOM
Updating(更新):正在被重新渲染
Unmounting(卸载):已移出真实 DOM
constructor():
在 React 组件挂载之前,会调用它的构造函数。
getDerivedStateFromProps():
在调用 render 方法之前调用,并且在初始挂载及后续更新时都会被调用。
render():
方法是class组件中唯一必须实现的方法。
componentDidMount():
在组件挂载后(插入 DOM 树中)立即调用。
getDerivedStateFromProps(): 在调用 render 方法之前调用,并且在初始挂载及后续更新时都会被调用。
根据 shouldComponentUpdate() 的返回值,判断 React 组件的输出是否受当前 state 或 props 更改的影响。
shouldComponentUpdate():当 props 或 state 发生变化时,shouldComponentUpdate() 会在渲染执行之前被调用。
render(): render() 方法是class组件中唯一必须实现的方法。
getSnapshotBeforeUpdate():在最近一次渲染输出(提交到 DOM 节点)之前调用。
componentDidUpdate():
在更新后会被立即调用。
componentWillUnmount():
在组件卸载及销毁之前直接调用。