From 4ca20fd36b2444a74279e7022f89894710b1daab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Markb=C3=A5ge?= Date: Wed, 17 Apr 2024 12:37:51 -0400 Subject: [PATCH] Test top level fragment inside lazy semantics (#28852) This wasn't clearly articulated and tested why the code structure is like this but I think the logic is correct - or at least consistent with the weird semantics. We place this top-level fragment check inside the recursion so that you can resolve how many every Lazy or Usable wrappers you want and it still preserves the same semantics if they weren't there (which they might not be as a matter of a race condition). However, we don't actually recurse with the top-level fragment unwrapping itself because nesting a bunch of keyless fragments isn't the same as a single fragment/element. --- .../react-reconciler/src/ReactChildFiber.js | 7 ++- .../src/__tests__/ReactFragment-test.js | 47 +++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/packages/react-reconciler/src/ReactChildFiber.js b/packages/react-reconciler/src/ReactChildFiber.js index 742206c47fbf5..e7534ac9e4347 100644 --- a/packages/react-reconciler/src/ReactChildFiber.js +++ b/packages/react-reconciler/src/ReactChildFiber.js @@ -1460,7 +1460,9 @@ function createChildReconciler( lanes: Lanes, debugInfo: ReactDebugInfo | null, ): Fiber | null { - // This function is not recursive. + // This function is only recursive for Usables/Lazy and not nested arrays. + // That's so that using a Lazy wrapper is unobservable to the Fragment + // convention. // If the top level item is an array, we treat it as a set of children, // not as a fragment. Nested arrays on the other hand will be treated as // fragment nodes. Recursion happens at the normal flow. @@ -1468,7 +1470,8 @@ function createChildReconciler( // Handle top level unkeyed fragments as if they were arrays. // This leads to an ambiguity between <>{[...]} and <>.... // We treat the ambiguous cases above the same. - // TODO: Let's use recursion like we do for Usable nodes? + // We don't use recursion here because a fragment inside a fragment + // is no longer considered "top level" for these purposes. const isUnkeyedTopLevelFragment = typeof newChild === 'object' && newChild !== null && diff --git a/packages/react-reconciler/src/__tests__/ReactFragment-test.js b/packages/react-reconciler/src/__tests__/ReactFragment-test.js index 3db7702f4cfef..fe1d1f4f953db 100644 --- a/packages/react-reconciler/src/__tests__/ReactFragment-test.js +++ b/packages/react-reconciler/src/__tests__/ReactFragment-test.js @@ -965,4 +965,51 @@ describe('ReactFragment', () => { , ); }); + + it('should preserve state of children when adding a fragment wrapped in Lazy', async function () { + const ops = []; + + class Stateful extends React.Component { + componentDidUpdate() { + ops.push('Update Stateful'); + } + + render() { + return
Hello
; + } + } + + const lazyChild = React.lazy(async () => ({ + default: ( + <> + +
World
+ + ), + })); + + function Foo({condition}) { + return condition ? : lazyChild; + } + + ReactNoop.render(); + await waitForAll([]); + + ReactNoop.render(); + await waitForAll([]); + + expect(ops).toEqual(['Update Stateful']); + expect(ReactNoop).toMatchRenderedOutput( + <> +
Hello
+
World
+ , + ); + + ReactNoop.render(); + await waitForAll([]); + + expect(ops).toEqual(['Update Stateful', 'Update Stateful']); + expect(ReactNoop).toMatchRenderedOutput(
Hello
); + }); });