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

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

相关推荐
kyriewen116 小时前
你点的“刷新”是假刷新?前端路由的瞒天过海术
开发语言·前端·javascript·ecmascript·html5
Timer@7 小时前
LangChain 教程 04|Agent 详解:让 AI 学会“自己干活“
javascript·人工智能·langchain
阿珊和她的猫8 小时前
TypeScript中的never类型: 深入理解never类型的使用场景和特点
javascript·typescript·状态模式
skywalk81638 小时前
Kotti Next的tinyfrontend前端模仿Kotti 首页布局还是不太好看,感觉比Kotti差一点
前端
RopenYuan10 小时前
FastAPI -API Router的应用
前端·网络·python
走粥10 小时前
clsx和twMerge解决CSS类名冲突问题
前端·css
Purgatory00110 小时前
layui select重新渲染
前端·layui
weixin1997010801611 小时前
《中国供应商商品详情页前端性能优化实战》
前端·性能优化
九皇叔叔12 小时前
003-SpringSecurity-Demo 统一响应类
java·javascript·spring·springsecurity