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

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

相关推荐
huohaiyu6 小时前
从URL到页面的完整解析流程
前端·网络·chrome·url
阿星AI工作室8 小时前
一个简单Demo彻底理解前后端怎么连的丨Figma + Supabase + Vercel
前端·人工智能
aircrushin8 小时前
一拍即传的平替,完全免费的实时照片墙!
前端
鹏北海10 小时前
JSBridge 原理详解
前端
孟健10 小时前
我的网站被黑了:一天灌入 227 万条垃圾数据,AI 写的代码差点让我社死
前端
anOnion10 小时前
构建无障碍组件之Checkbox pattern
前端·html·交互设计
IT枫斗者12 小时前
IntelliJ IDEA 2025.3史诗级更新:统一发行版+Spring Boot 4支持,这更新太香了!
java·开发语言·前端·javascript·spring boot·后端·intellij-idea
N***p36512 小时前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
NGC_661112 小时前
二分查找算法
java·javascript·算法
享誉霸王13 小时前
15、告别混乱!Vue3复杂项目的规范搭建与基础库封装实战
前端·javascript·vue.js·前端框架·json·firefox·html5