didact icon indicating copy to clipboard operation
didact copied to clipboard

commitWork function should return after commitDeletion(anOldFiber, domParent)

Open 1adybug opened this issue 3 years ago • 0 comments

function commitWork(fiber) {
  if (!fiber) {
    return
  }

  let domParentFiber = fiber.parent
  while (!domParentFiber.dom) {
    domParentFiber = domParentFiber.parent
  }
  const domParent = domParentFiber.dom

  if (
    fiber.effectTag === "PLACEMENT" &&
    fiber.dom != null
  ) {
    domParent.appendChild(fiber.dom)
  } else if (
    fiber.effectTag === "UPDATE" &&
    fiber.dom != null
  ) {
    updateDom(
      fiber.dom,
      fiber.alternate.props,
      fiber.props
    )
  } else if (fiber.effectTag === "DELETION") {
    commitDeletion(fiber, domParent)

    // function should return at this moment
    return
  }

  commitWork(fiber.child)
  commitWork(fiber.sibling)
}

let's call the fiber to be deleted oldFiber

if commitWork function didnt return after commitDeletion(oldFiber, domParent), it will commitWork(oldFiber.child) and commitWork(oldFiber.sibling).

This may cause a terrible bug, because u dont know what type is oldFiber.child.effectTag or oldFiber.sibiling.effectTag

and commitWork function dont know the two fibers are old fibers and will treat them as current fiber to process.

1adybug avatar Aug 25 '22 19:08 1adybug