Skip to content

Commit

Permalink
五: 4.Placement — 添加 dom
Browse files Browse the repository at this point in the history
  • Loading branch information
zh-lx committed Nov 10, 2021
1 parent 538421d commit d5d5f6e
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/mini-react/commit.js
Expand Up @@ -11,6 +11,17 @@ function commitWork(fiber) {
// 深度优先遍历,先遍历 child,后遍历 sibling
commitWork(fiber.child);
let parentDom = fiber.return.stateNode;
parentDom.appendChild(fiber.stateNode);
if (fiber.flag === 'Placement') {
// 添加 dom
const targetPositionDom = parentDom.childNodes[fiber.index]; // 要插入到那个 dom 之前
if (targetPositionDom) {
// targetPositionDom 存在,则插入
parentDom.insertBefore(fiber.stateNode, targetPositionDom);
} else {
// targetPositionDom 不存在,插入到最后
parentDom.appendChild(fiber.stateNode);
}
}

commitWork(fiber.sibling);
}

0 comments on commit d5d5f6e

Please sign in to comment.