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() 用于错误边界。

这些生命周期方法让开发者可以在不同阶段插入自定义逻辑,增强组件的行为和性能。

相关推荐
ZC跨境爬虫5 小时前
跟着 MDN 学 HTML day_37:(深入掌握 CustomEvent 自定义事件接口)
前端·javascript·ui·html·音视频
whinc12 小时前
JavaScript技术周刊 2026年第18周
javascript
码海扬帆:前端探索之旅12 小时前
深度定制 uni-combox:新增功能详解与实战指南
前端·vue.js·uni-app
谷雨不太卷12 小时前
进程的状态码
java·前端·算法
打小就很皮...12 小时前
基于 Python + LangChain + RAG 的知识检索系统实战
前端·langchain·embedding·rag