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
    © 著作权归作者所有,转载或内容合作请联系作者
    平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务
相关推荐
巧妹儿4 分钟前
AI Agent 实战:MySQL 监控指标查询 Skill|华为云 + 腾讯云双兼容可直接复用
python·mysql·ai·大模型·华为云·腾讯云
w6100104665 分钟前
CKAD-2026-Secret
运维·k8s·ckad
Zww08917 分钟前
idea配置注释模板
java·ide·intellij-idea
无巧不成书02187 分钟前
基于WSL 2的Docker远程开发全栈实战指南
运维·docker·容器·docker desktop·wsl 2·vs code远程开发·容器化开发
Renhao-Wan9 分钟前
Docker 核心原理详解:镜像、容器、Namespace、Cgroups 与 UnionFS
java·后端·docker·容器
一个天蝎座 白勺 程序猿10 分钟前
踩坑生产后整理:KingbaseES表空间管理、auto_createtblspcdir参数深度解析与运维最佳实践
运维·数据库·kingbasees
赵庆明老师11 分钟前
Linux Docker打包
linux·运维·docker
Eloudy13 分钟前
docker pull ubuntu:22.04 失败的解决记录
运维·docker·容器
Rsun0455116 分钟前
ScheduledExecutorService类作用
java
Wait....21 分钟前
MySQL事务知识复习
数据库·mysql