-
Notifications
You must be signed in to change notification settings - Fork 14
leetcode-js/react/12 #10
-
leetcode-js/react/12
- 实现 Diff 算法 摘要 Diff 算法简介 实现单节点 Diff 实现多节点 Diff 处理 commit 阶段 相关代码可在 git tag v1.12 查看
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 2 comments 1 reply
-
你好 我想请教下
appendPlacementNodeIntoContainer 方法中有一个if判断,
if判断 dom类型的fiber节点,
else中如果是个 函数 fiber节点,遍历子节点。那么 之前if中append的节点 不是会重新append 一边吗?
Beta Was this translation helpful? Give feedback.
All reactions
-
appendPlacementNodeIntoContainer
的作用是将完成渲染的 FiberNode
以及其子节点,插入到 DOM 容器中,分两种情况:
-
如果
finishedWork
是HostComponent
或HostText
(即 DOM 节点或文本节点),则进行移动或插入操作; -
如果
finishedWork
有子节点,且该节点不是HostComponent
或HostText
类型,说明它是某种复合组件,通常是自定义的 React 组件或某些内部的特殊节点(例如Fragment
、ContextProvider
等)。这些节点本身不会直接对应 DOM 元素,因此需要递归遍历其子节点,直到找到可以映射到真实 DOM(即HostComponent
或HostText
)的节点。
举个例子,以下 React 组件结构:
function MyComponent() { return ( <div> <span>Hello</span> <>World</> </div> ); }
React 组件返回的 JSX 包含一个空的 Fragment
(<>World</>
)。这个情况下,React Fiber 树中会有一个 Fragment
节点,但它不会直接映射到实际的 DOM 元素。
MyComponent
的 Fiber 树结构会像这样:
finishedWork
(MyComponent
的 Fiber)child
→<div>
的 Fiber(HostComponent
)child
→<span>
的 Fiber(HostComponent
)child
→ "Hello" 的 Fiber(HostText
)
sibling
→Fragment
的 Fiberchild
→ "World" 的 Fiber(HostText
)
遍历过程:
-
MyComponent
的 Fiber:finishedWork.tag
不是HostComponent
或HostText
,因此不会直接插入到 DOM 中。- 递归处理
MyComponent
的child
,即<div>
的 Fiber。
-
<div>
的 Fiber:finishedWork.tag
是HostComponent
,表示这是一个真实的 DOM 元素(<div>
)。appendChildToContainer
将<div>
插入到hostParent
容器中。
-
递归处理
<div>
的子节点:- 处理
<div>
的child
,即<span>
的 Fiber。 <span>
是HostComponent
,它被插入到<div>
中。- 递归处理
span
的子节点——文本节点 "Hello"。
- 处理
-
处理文本节点 "Hello" 的 Fiber:
finishedWork.tag
是HostText
,表示这是一个文本节点,它直接插入到<span>
中。
-
处理
Fragment
的 Fiber:Fragment
的Fiber
节点(<>World</>
)不会映射到 DOM,因为它仅用于结构化子节点。- 继续递归处理
Fragment
的child
,即文本节点 "World"。
-
处理文本节点 "World" 的 Fiber:
- 该
Fiber
是HostText
类型,表示它是一个文本节点,因此被插入到<div>
中,作为最后一个子节点。
- 该
最终的 DOM 结构如下:
<div> <span>Hello</span> World </div>
大概是这么一个过程,本质就是深度优先遍历一棵树,所以【之前 if 中 append 的节点不会重新 append 一遍】
Beta Was this translation helpful? Give feedback.
All reactions
-
Beta Was this translation helpful? Give feedback.