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
    © 著作权归作者所有,转载或内容合作请联系作者
    平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务
相关推荐
赵文宇几秒前
站在MySQL肩膀上快速入门PostgreSQL,开源社区最喜爱的关系数据库
mysql·postgresql
Homeey2 分钟前
深入理解ThreadLocal:从原理到架构实践的全面解析
java·后端
ANGLAL4 分钟前
27.短链系统
java
Gold Steps.7 分钟前
数据库正常运行但是端口变成了0?
数据库·mysql
破烂pan19 分钟前
Jenkins 定时触发(cron)使用说明
运维·jenkins
Lisonseekpan22 分钟前
Git 命令大全:从基础到高级操作
java·git·后端·github·团队开发
CodeCraft Studio37 分钟前
国产化Excel处理控件Spire.XLS教程:使用Java将CSV转换为PDF(含格式设置)
java·pdf·excel·spire.xls·文档格式转换·csv转pdf
乐悠小码1 小时前
Java设计模式精讲---04原型模式
java·设计模式·原型模式
秋风&萧瑟1 小时前
【C++】智能指针介绍
java·c++·算法
QiZhang | UESTC1 小时前
JAVA算法练习题day67
java·python·学习·算法·leetcode