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
    © 著作权归作者所有,转载或内容合作请联系作者
    平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务
相关推荐
༾冬瓜大侠༿5 分钟前
C语言:自定义类型——联合体和枚举
java·c语言·开发语言
yuriy.wang28 分钟前
Spring IOC源码篇五 核心方法obtainFreshBeanFactory.doLoadBeanDefinitions
java·后端·spring
凸头33 分钟前
解决慢SQL问题
java·mysql
lang2015092833 分钟前
MySQL缓冲池秒热技巧:告别冷启动
数据库·mysql
脑壳疼___1 小时前
若依 springboot websocket
java·spring
空灵之海2 小时前
Docker部署Nexus Repository Community Edition
运维·docker·容器
aramae2 小时前
Linux开发工具入门:零基础到熟练使用(二)
linux·运维·服务器·网络·笔记
咖啡教室2 小时前
程序员应该掌握的网络命令telnet、ping和curl
运维·后端
优秘智能UMI3 小时前
UMI企业智脑智能营销:多平台视频矩阵引领营销新潮流
大数据·运维·人工智能·ai·矩阵·aigc
失散134 小时前
分布式专题——23 Kafka日志索引详解
java·分布式·云原生·架构·kafka