Deep Dive React 4 How does React State actually work

In other word, how does React handle updates?

How React communicates with the renderer

复制代码
setState(...args) {
  this.updater.enqueueSetState(this, ...args);
}

The Update

  • When we call setState, React adds the passed data to a queue
  • The updates are later handled one by one, but the changes are applied at the same time at the end (flushed to the DOM at once)

Lifecycle & 2 phases


Screen Shot 2022-08-18 at 9.12.01 PM.png

复制代码
static getDerivedStateFromProps(props, state) {
  console.log('this is the only static lifecycle methods, cannot access this, window');
  return state;
}

Update Steps

happend in 2 phases

  1. setState is called
  2. data is added to a queue
  3. React starts handling the update
  4. new state is calculated + getDerivedStateFromProps is called
  5. shouldComponentUpdate is called with final (derived) state
  6. render is called

  1. DOM changes are made
  2. componentDidUpdate is called
    © 著作权归作者所有,转载或内容合作请联系作者
    平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务
相关推荐
老K的Java兵器库7 分钟前
集合性能基准测试报告:ArrayList vs LinkedList、HashMap vs TreeMap、并发 Map 四兄弟
java·开发语言
Knight_AL11 分钟前
如何解决 Jacob 与 Tomcat 类加载问题:深入分析 Tomcat 类加载机制与 JVM 双亲委派机制
java·jvm·tomcat
java水泥工20 分钟前
旅游管理系统|基于SpringBoot和Vue的旅游管理系统(源码+数据库+文档)
spring boot·vue·计算机毕业设计·java毕业设计·旅游管理系统
草莓熊Lotso21 分钟前
Linux 进阶指令实操指南:文件查看、时间管理、搜索压缩全场景覆盖(附高频案例)
linux·运维·服务器
Cx330❀22 分钟前
《Linux进阶指令实操指南》:文件查看、时间管理、搜索压缩全覆盖(附高频案例)
linux·运维·服务器
哲学七24 分钟前
Springboot3.5.x版本引入javaCv相关库版本问题以及精简引入包
java·ffmpeg
Aqua Cheng.39 分钟前
代码随想录第七天|哈希表part02--454.四数相加II、383. 赎金信、15. 三数之和、18. 四数之和
java·数据结构·算法·散列表
Nebula_g40 分钟前
Java哈希表入门详解(Hash)
java·开发语言·学习·算法·哈希算法·初学者