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
    © 著作权归作者所有,转载或内容合作请联系作者
    平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务
相关推荐
NE_STOP16 分钟前
MyBatis-配置文件解读及MyBatis为何不用编写Mapper接口的实现类
java
梦想很大很大2 小时前
拒绝“盲猜式”调优:在 Go Gin 项目中落地 OpenTelemetry 链路追踪
运维·后端·go
Sinclair3 小时前
内网服务器离线安装 Nginx+PHP+MySQL 的方法
运维
叶落阁主4 小时前
Tailscale 完全指南:从入门到私有 DERP 部署
运维·安全·远程工作
后端AI实验室5 小时前
用AI写代码,我差点把漏洞发上线:血泪总结的10个教训
java·ai
用户8307196840826 小时前
MySQL 查询优化 30 条封神技巧:用好索引,少耗资源,查询快到飞起
mysql
程序员清风7 小时前
小红书二面:Spring Boot的单例模式是如何实现的?
java·后端·面试
belhomme7 小时前
(面试题)Redis实现 IP 维度滑动窗口限流实践
java·面试
Be_Better7 小时前
学会与虚拟机对话---ASM
java
Nyarlathotep01138 小时前
事务隔离级别
sql·mysql