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
    © 著作权归作者所有,转载或内容合作请联系作者
    平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务
相关推荐
小刘数字化14 分钟前
AR数字孪生:重塑工业运维的下一代交互范式
运维·ar
咖啡八杯17 分钟前
文法、BNF与AST
java·设计模式·解释器模式·ast·文法
翰德恩咨询26 分钟前
华为DSTE咨询洞察:如何规划人才支撑战略落地
运维·服务器·华为
要开心吖ZSH2 小时前
一文搞懂:JDK 21 虚拟线程 vs N种异步方案,到底该怎么选?
java·数据库·jdk·虚拟线程
guodingdingh7 小时前
软件开发工作问题总结0718
java·开发语言·数据库
咖啡八杯9 小时前
GoF设计模式——解释器模式
java·后端·spring·设计模式
优橙教育9 小时前
5G网优培训 vs Java开发:转行选哪个?
java·开发语言·5g
糖果店的幽灵9 小时前
【DeepAgents 从入门到精通】Context Management 上下文管理
java·人工智能·后端·spring·中间件·langgraph·deepagents
腻害兔11 小时前
【若依项目-产品经理视角】RuoYi-Vue-Pro 源码拆解:字典、短信、邮件、通知——后台系统的“基础设施四件套“!
java·前端·vue.js·产品经理·ai编程
CodexDave11 小时前
MySQL事务隔离级别与MVCC机制解析
前端·数据库·mysql·nginx·性能优化·负载均衡