GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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
Filter options
  • packages/react-reconciler/src
    • ReactChildFiber.js
    • __tests__
      • ReactFragment-test.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/react-reconciler/src/ReactChildFiber.js‎

Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactChildFiber.js
+5-2Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,15 +1460,18 @@ function createChildReconciler(
14601460
lanes: Lanes,
14611461
debugInfo: ReactDebugInfo | null,
14621462
): 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.
14641466
// If the top level item is an array, we treat it as a set of children,
14651467
// not as a fragment. Nested arrays on the other hand will be treated as
14661468
// fragment nodes. Recursion happens at the normal flow.
14671469

14681470
// Handle top level unkeyed fragments as if they were arrays.
14691471
// This leads to an ambiguity between <>{[...]}</> and <>...</>.
14701472
// 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.
14721475
const isUnkeyedTopLevelFragment =
14731476
typeof newChild === 'object' &&
14741477
newChild !== null &&
Collapse file

‎packages/react-reconciler/src/__tests__/ReactFragment-test.js‎

Copy file name to clipboardExpand all lines: packages/react-reconciler/src/__tests__/ReactFragment-test.js
+47Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,4 +965,51 @@ describe('ReactFragment', () => {
965965
</>,
966966
);
967967
});
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+
});
9681015
});

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.