Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Test top level fragment inside lazy semantics #28852

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/react-reconciler/src/ReactChildFiber.js
Original file line number Diff line number Diff line change
Expand Up @@ -1460,15 +1460,18 @@ 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.

// 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 &&
Expand Down
47 changes: 47 additions & 0 deletions packages/react-reconciler/src/__tests__/ReactFragment-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <div>Hello</div>;
}
}

const lazyChild = React.lazy(async () => ({
default: (
<>
<Stateful key="a" />
<div key="b">World</div>
</>
),
}));

function Foo({condition}) {
return condition ? <Stateful key="a" /> : lazyChild;
}

ReactNoop.render(<Foo condition={true} />);
await waitForAll([]);

ReactNoop.render(<Foo condition={false} />);
await waitForAll([]);

expect(ops).toEqual(['Update Stateful']);
expect(ReactNoop).toMatchRenderedOutput(
<>
<div>Hello</div>
<div>World</div>
</>,
);

ReactNoop.render(<Foo condition={true} />);
await waitForAll([]);

expect(ops).toEqual(['Update Stateful', 'Update Stateful']);
expect(ReactNoop).toMatchRenderedOutput(<div>Hello</div>);
});
});