Comments (4)
@Genitana
- 其中fiber(E)为什么可以复用?老序列中fiber(E)节点的key是 key=d , 而新节点 key=e,key不一样,作者是笔误了吗?
- fiber(E)可以复用,但为什么没有位移?老序列中fiber(E)在第5个位置,而新的ReactELement序列中E在第3个位置,明显位置不一样,为什么只有fiber(C)位移了?fiber(E)不应该也位移了吗?
比较尴尬啊, 确实是图画错了, key=e
才对. 关于节点移动的问题 @zhangyu1818 ,已经解答过了, 简单明了.
可以简单看一下源码: 判断位置移动是在 placeChild(newFiber, lastPlacedIndex, newIdx)
中, 由于篇幅原因(且该函数代码量不大), 所以没有展开细节.
function placeChild(
newFiber: Fiber,
lastPlacedIndex: number,
newIndex: number,
): number {
newFiber.index = newIndex;
if (!shouldTrackSideEffects) {
// Noop.
return lastPlacedIndex;
}
const current = newFiber.alternate;
if (current !== null) {
const oldIndex = current.index;
if (oldIndex < lastPlacedIndex) {
// This is a move.
newFiber.flags = Placement;
return lastPlacedIndex;
} else {
// This item can stay in place.
return oldIndex;
}
} else {
// This is an insertion.
newFiber.flags = Placement;
return lastPlacedIndex;
}
}
lastPlacedIndex
初始值为0, 代表最近一个被移动节点的位置.
- 比较节点是否移动, 只发生在第二次循环过程中(公共序列之后的剩余序列的比较), 理论上剩余序列是乱序的.
- 从左到右遍历剩余序列, 判断节点是否可以复用; 如果可以复用, 再判定是否移动.
- 本例中fiber(E)被判断为可复用, 又因为它位于剩余序列中(且是可复用节点)的第一个, 且这时
if (oldIndex < lastPlacedIndex)
不成立. 故不用标记placement
. 返回后lastPlacedIndex = oldIndex
- 当后面再遇到可复用节点之后, 比较
fiber.alternate.index
与lastPlacedIndex
位置, 只要当前可复用节点在前一可复用节点右边, 就没有位移(还按照原有顺序). 如果后者在前者左边(与原有顺序不同), 则标记placement
.
- 本例中fiber(E)被判断为可复用, 又因为它位于剩余序列中(且是可复用节点)的第一个, 且这时
from react-illustration-series.
- 看上去是图上画错了,图上有2个key=d的节点,最后一个应该是key=e,key相同是复用的。
- 多节点diff移动都是统一向右移动的,新的e是第一个,不用移动,需要把c移动到右边,作者这一点没有讲,可以看这里
from react-illustration-series.
好的,非常感谢解答疑惑
from react-illustration-series.
基本原理=>
而是旧fiber对象与新ReactElement对象 向 相比较
from react-illustration-series.
Related Issues (20)
- 原理解析的一些误点? HOT 1
- fiber树构造阶段的问题
- fiber树构造(对比更新) -> 过程图解 -> performUnitOfWork第 4 次调用(执行beginWork和completeUnitOfWork)
- 移动端页面有问题,无法导航 HOT 2
- 网站好像挂了 HOT 2
- 网站中“运行核心”部分章节顺序错乱
- 网站的标题定位有点问题
- 链表操作的图是不是画错了 HOT 1
- hook-state 一节中 baseQueue拼接后的链表顺序好像有误
- 同学,18有计划吗 HOT 3
- 请问博客的生成框架用的是哪一个 HOT 2
- 请问下博主,是不是现在的16,17其实都是同步循环 HOT 3
- 啥时候更新React18呢
- fiber 优先级 HOT 1
- 图画错了 HOT 1
- commitBeforeMutationEffects 里处理的是哪些 Hook ? HOT 1
- mime模块拒绝服务漏洞 - [email protected] - 间接引入 HOT 2
- Can we port this to English in a fork HOT 6
- effect hooks图例的构造顺序是否有问题 HOT 2
- 反转链表代码有误 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-illustration-series.