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
    © 著作权归作者所有,转载或内容合作请联系作者
    平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务
相关推荐
catcfm1 小时前
Java学习笔记-泛型
java·笔记·学习
柏油2 小时前
MySQL InnoDB 后台线程
数据库·后端·mysql
老华带你飞2 小时前
社区互助|基于SSM+vue的社区互助平台的设计与实现(源码+数据库+文档)
java·前端·数据库·vue.js·小程序·毕设·社区互助平台
007php0073 小时前
Go 错误处理:用 panic 取代 err != nil 的模式
java·linux·服务器·后端·ios·golang·xcode
阿华的代码王国3 小时前
【Android】OkHttp发起GET请求 && POST请求
android·java·okhttp·网络连接
孙克旭_3 小时前
day082-初识ElasticStack
linux·运维·elasticsearch
island13143 小时前
【Redis#7】Redis 数据结构 -- Set 类型
java·数据结构·redis
3Cloudream3 小时前
互联网大厂Java面试:从基础到微服务的深度解析
java·spring·微服务·面试·技术解析·电商场景
接着奏乐接着舞。3 小时前
前端RSA加密遇到Java后端解密失败的问题解决
java·开发语言·前端