facebook / react

The library for web and native user interfaces.
https://react.dev
MIT License
226.65k stars 46.23k forks source link

Question: Why `propagateContextChanges` not restore the `workInProgress.child.return` pointer? #22012

Open 7kms opened 3 years ago

7kms commented 3 years ago

This is the code from ReactFiberNewContext.old.js

https://github.com/facebook/react/blob/19092ac8c354b92c2e0e27b73f391571ad452505/packages/react-reconciler/src/ReactFiberNewContext.old.js#L319-L333

In the function propagateContextChanges and propagateContextChange_eager, the workInProgress.child.return is changed in the beginning, but it is not restore in the end.

In case of propagateContextChanges:

function propagateContextChanges<T>(
  workInProgress: Fiber,
  contexts: Array<any>,
  renderLanes: Lanes,
  forcePropagateEntireTree: boolean,
): void {
  // Only used by lazy implemenation
  if (!enableLazyContextPropagation) {
    return;
  }
  let fiber = workInProgress.child;
  if (fiber !== null) {
    //  caution:  before next line , fiber.return is workInProgress.alternate
    fiber.return = workInProgress;
   // after this line, fiber.return = workInProgress
  }

  // ... omit the other code
  // but in the end, there is no code to restore the  workInProgress.child.return
  // maybe like this:
  workInProgress.child.return = workInProgress.alternate;
}

I think the workInProgress.child is same as workInProgress.alternate.child at this time.

If the workInProgress.child.return not restore, though the workInProgress tree is no difference , but maybe the current tree is broken?

I'm confused about it some days, could anyone explain it?

7kms commented 3 years ago

@acdlite , @gnoff could you give some help?