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
    © 著作权归作者所有,转载或内容合作请联系作者
    平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务
相关推荐
Jason?134 分钟前
Unity基于Recoder的API写了一个随时录屏的工具
java·unity·游戏引擎
都叫我大帅哥4 分钟前
🐇 RabbitMQ延时队列:让消息学会“踩点上班”的终极指南
java·rabbitmq
JiaHao汤30 分钟前
Java 虚拟机之双亲委派机制
java·jvm·后端
哈基米喜欢哈哈哈34 分钟前
Uber的MySQL实践(一)——学习笔记
数据库·笔记·后端·mysql
C4程序员1 小时前
北京JAVA基础面试30天打卡03
java·开发语言·面试
程序员黄老师1 小时前
Linux tail 命令详解
linux·运维
哥哥还在IT中1 小时前
MVCC 实现之探析
数据库·mysql·tidb
我怎么又饿了呀2 小时前
Windows&Linux系统 安装 CUDA 和 cuDNN
linux·运维·windows
Java技术小馆2 小时前
PromptPilot打造高效AI提示词
java·后端·面试
whysqwhw2 小时前
线程池数量配置
java