Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Why can not use '!==' here ? #36

Open
Ha0ran2001 opened this issue Aug 19, 2021 · 3 comments
Open

Why can not use '!==' here ? #36

Ha0ran2001 opened this issue Aug 19, 2021 · 3 comments

Comments

@Ha0ran2001
Copy link

function reconcileChildren(wipFiber, elements) {
  let index = 0;
  let oldFiber = wipFiber.alternate && wipFiber.alternate.child;
  let prevSibling = null;

  while (index < elements.length || oldFiber != null) {
    const element = elements[index];
    let newFiber = null;

    const sameType = oldFiber && element && element.type == oldFiber.type;

    if (sameType) {
      newFiber = {
        type: oldFiber.type,
        props: element.props,
        dom: oldFiber.dom,
        parent: wipFiber,
        alternate: oldFiber,
        effectTag: "UPDATE"
      };
    }
    if (element && !sameType) {
      newFiber = {
        type: element.type,
        props: element.props,
        dom: null,
        parent: wipFiber,
        alternate: null,
        effectTag: "PLACEMENT"
      };
    }
    if (oldFiber && !sameType) {
      oldFiber.effectTag = "DELETION";
      deletions.push(oldFiber);
    }

    if (oldFiber) {
      oldFiber = oldFiber.sibling;
    }

    if (index === 0) {
      wipFiber.child = newFiber;
    } else if (element) {
      prevSibling.sibling = newFiber;
    }

    prevSibling = newFiber;
    index++;
  }
}

When I use '!==' like

 while (index < elements.length || oldFiber !== null) {
...
}

and When I input and then I can't continue typing, the browser is also stuck. Why ?

@iszhangjin
Copy link

"wipFiber.alternate.child" may be 'undefined'

@uptonking
Copy link

I ran into this problem as well.

 while (index < elements.length || oldFiber) {

simpler slution 🤗️

@1111PP
Copy link

1111PP commented Oct 12, 2024

because oldFiber maybe is undefined , and undefined !==null always return true ,then endless loop

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants