GeezerVison is showing github.com — Test top level fragment inside lazy semantics (#28852) · react/react@4ca20fd
File tree
Expand file treeCollapse file tree
Open diff view settings
packages/react-reconciler/src
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactChildFiber.js +5-2Lines changed: 5 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1460,15 +1460,18 @@ function createChildReconciler( |
1460 | 1460 | lanes: Lanes, |
1461 | 1461 | debugInfo: ReactDebugInfo | null, |
1462 | 1462 | ): Fiber | null { |
1463 | | - // This function is not recursive. |
| 1463 | + // This function is only recursive for Usables/Lazy and not nested arrays. |
| 1464 | + // That's so that using a Lazy wrapper is unobservable to the Fragment |
| 1465 | + // convention. |
1464 | 1466 | // If the top level item is an array, we treat it as a set of children, |
1465 | 1467 | // not as a fragment. Nested arrays on the other hand will be treated as |
1466 | 1468 | // fragment nodes. Recursion happens at the normal flow. |
1467 | 1469 |
|
1468 | 1470 | // Handle top level unkeyed fragments as if they were arrays. |
1469 | 1471 | // This leads to an ambiguity between <>{[...]}</> and <>...</>. |
1470 | 1472 | // We treat the ambiguous cases above the same. |
1471 | | - // TODO: Let's use recursion like we do for Usable nodes? |
| 1473 | + // We don't use recursion here because a fragment inside a fragment |
| 1474 | + // is no longer considered "top level" for these purposes. |
1472 | 1475 | const isUnkeyedTopLevelFragment = |
1473 | 1476 | typeof newChild === 'object' && |
1474 | 1477 | newChild !== null && |
|
Collapse file
Copy file name to clipboardExpand all lines: packages/react-reconciler/src/__tests__/ReactFragment-test.js +47Lines changed: 47 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -965,4 +965,51 @@ describe('ReactFragment', () => { |
965 | 965 | </>, |
966 | 966 | ); |
967 | 967 | }); |
| 968 | + |
| 969 | + it('should preserve state of children when adding a fragment wrapped in Lazy', async function () { |
| 970 | + const ops = []; |
| 971 | + |
| 972 | + class Stateful extends React.Component { |
| 973 | + componentDidUpdate() { |
| 974 | + ops.push('Update Stateful'); |
| 975 | + } |
| 976 | + |
| 977 | + render() { |
| 978 | + return <div>Hello</div>; |
| 979 | + } |
| 980 | + } |
| 981 | + |
| 982 | + const lazyChild = React.lazy(async () => ({ |
| 983 | + default: ( |
| 984 | + <> |
| 985 | + <Stateful key="a" /> |
| 986 | + <div key="b">World</div> |
| 987 | + </> |
| 988 | + ), |
| 989 | + })); |
| 990 | + |
| 991 | + function Foo({condition}) { |
| 992 | + return condition ? <Stateful key="a" /> : lazyChild; |
| 993 | + } |
| 994 | + |
| 995 | + ReactNoop.render(<Foo condition={true} />); |
| 996 | + await waitForAll([]); |
| 997 | + |
| 998 | + ReactNoop.render(<Foo condition={false} />); |
| 999 | + await waitForAll([]); |
| 1000 | + |
| 1001 | + expect(ops).toEqual(['Update Stateful']); |
| 1002 | + expect(ReactNoop).toMatchRenderedOutput( |
| 1003 | + <> |
| 1004 | + <div>Hello</div> |
| 1005 | + <div>World</div> |
| 1006 | + </>, |
| 1007 | + ); |
| 1008 | + |
| 1009 | + ReactNoop.render(<Foo condition={true} />); |
| 1010 | + await waitForAll([]); |
| 1011 | + |
| 1012 | + expect(ops).toEqual(['Update Stateful', 'Update Stateful']); |
| 1013 | + expect(ReactNoop).toMatchRenderedOutput(<div>Hello</div>); |
| 1014 | + }); |
968 | 1015 | }); |