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
    © 著作权归作者所有,转载或内容合作请联系作者
    平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务
相关推荐
传奇开心果编程22 分钟前
【springboot基础语法学与练】第 1 课:从零开始
java·spring boot·后端·学习
wdfk_prog22 分钟前
ROS教程08:从 TransportTCP::connect() 追到 TCPROS Connection Header、序列化与 Socket 数据传输
运维·缓存·docker·容器·ros
SL_staff25 分钟前
财务系统慎用低代码?从数据模型闭环看合规落地的技术实践
java·低代码·全栈
滕州市燕猫虎计算机科技工作室个体工商户33 分钟前
IDEA:Command line is too long
java·ide·intellij-idea
做运维的阿瑞34 分钟前
Python 标准库汇总:分类速览与常用模块清单
linux·运维·python
百万蹄蹄向前冲1 小时前
密码都对Node.js却连不上Linux数据库
linux·mysql
华章酱1 小时前
MySQL幻读是怎么出现的
数据库·mysql·幻读
步行cgn1 小时前
Spring p 命名空间注入详解
java·前端·spring
YatHinLay1 小时前
MyBatis 流式查询实战:ResultHandler 处理海量数据
java
浅念-1 小时前
Redis基础详解:单线程模型、String与Hash数据结构
服务器·数据库·redis·sql·mysql·nosql数据库·nosql