Bootstrap

React的生命周期?

React的生命周期分为三个主要阶段:挂载(Mounting)、更新(Updating)和卸载(Unmounting)。

1、挂载(Mounting)

当组件实例被创建并插入 DOM 时调用的生命周期方法:

constructor()
static getDerivedStateFromProps()
render()
componentDidMount()

2、更新(Updating)

当组件的状态或属性发生变化时调用的生命周期方法:

static getDerivedStateFromProps()
shouldComponentUpdate()
render()
getSnapshotBeforeUpdate()
componentDidUpdate()

3、卸载(Unmounting)

当组件从 DOM 中移除时调用的生命周期方法:

componentWillUnmount()
其他
componentDidCatch() 和 static getDerivedStateFromError() 用于错误边界。
这些生命周期方法让开发者可以在不同阶段插入自定义逻辑,增强组件的行为和性能。

;