-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f86d5e9
commit 1a6d749
Showing
13 changed files
with
187 additions
and
9,080 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
# dependencies | ||
/node_modules | ||
/pics | ||
/.pnp | ||
.pnp.js | ||
.idea | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8,783 changes: 0 additions & 8,783 deletions
8,783
docs/render阶段/beginWork阶段/setState异步原理/multiSetState.json
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,38 @@ | ||
React 进入调度有两种途径:挂载和更新,可以把他们理解成任务,调度的目的是依据优先级对任务进行合理的安排。 | ||
|
||
React 的Concurrent模式解决的问题是帮助应用保持响应,而交互和更新任务是互斥的。交互的最终结果是产生更新,进而生成任务进入调度流程。 | ||
|
||
如果此时已有任务在进行,但优先级低于新任务,那么它将会被取消,重新对新任务安排一个调度。 | ||
|
||
这里说的任务在React的内在行为上,就是从root开始,进入工作循环,构建workInProgress树。 | ||
|
||
任务调度 | ||
|
||
工作循环 | ||
概述 | ||
执行工作单元 | ||
render阶段 | ||
beginWork | ||
处理更新 | ||
diff操作 | ||
completeWork | ||
commit阶段 | ||
commitWork | ||
|
||
更新的完整过程 | ||
|
||
|
||
金字塔结构 | ||
|
||
React 第一次渲染: ReactDOM.render | ||
数据准备 调度 workLoop | ||
|
||
React 更新: ReactDOM.render | ||
事件系统 调度 workLoop | ||
|
||
|
||
|
||
React原理,从更新说起。 | ||
|
||
作为一个构建用户界面的库,React的核心始终围绕着更新这一个重要的目标,将更新和极致的用户体验结合起来是React团队一直在努力的事情。为什么React可以将用户体验做到这么好?我想这是基于以下两点原因: | ||
|
||
* Fiber架构和Scheduler出色的调度模式可以实现异步可中断的更新行为。 | ||
* 优先级机制贯穿更新的整个周期 | ||
|
||
下面我们基于这两点展开,在解读原理之前,对一些概念先有个基础认知。 | ||
# Fiber是什么 | ||
Fiber是什么?它是React的最小工作单元,在React的世界中,一切都可以是组件。在普通的HTML页面上,人为地将多个DOM元素整合在一起可以组成一个组件,HTML标签可以是组件(HostComponent), | ||
普通的文本节点也可以是组件(HostText)。每一个组件就对应着一个fiber节点,许多个fiber节点互相嵌套、关联,就组成了fiber树,正如下面表示的Fiber树和DOM的关系一样: | ||
``` | ||
Fiber树 DOM树 | ||
div#root div#root | ||
| | | ||
<App/> div | ||
| / \ | ||
div p a | ||
/ ↖ | ||
/ ↖ | ||
p ----> <Child/> | ||
| | ||
a | ||
``` | ||
一个DOM节点一定对应着一个Fiber节点,但一个Fiber节点却不一定有对应的DOM节点。 | ||
|
||
每一个fiber节点都可能会产生更新,每当有更新的时候,React都会从root开始重新构建fiber树。这个行为,就是React的更新任务。对Fiber树来说,一旦有一个更新任务,它就会经历一个深度优先遍历的过程, | ||
依照现有fiber树构建一个新的fiber树,现有fiber树称为current树,构建的新fiber树称为workInProgress树。整个fiber树会经历向下遍历以及向上回溯的两个过程,所以每个fiber节点,也会经历两个过程: | ||
**beginWork**和**completeWork**。**构建过程中会有一个workInProgress的指针记录下当前构建到哪个fiber节点**,这是React更新任务可恢复的重要原因之一。fiber树的构建过程如下动图: | ||
|
||
 | ||
|
||
|
||
# 双缓冲机制 | ||
双缓冲机制 | ||
|
Oops, something went wrong.