Skip to content

Commit 6b46ad9

Browse files
committed
Fix key Warning for Flattened Positional Children
1 parent c2b45ef commit 6b46ad9

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

packages/react/src/__tests__/ReactChildren-test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,25 @@ describe('ReactChildren', () => {
868868
]);
869869
});
870870

871+
it('does not warn for flattened positional children', async () => {
872+
function ComponentRenderingFlattenedChildren({children}) {
873+
return <div>{React.Children.toArray(children)}</div>;
874+
}
875+
876+
const container = document.createElement('div');
877+
const root = ReactDOMClient.createRoot(container);
878+
await expect(async () => {
879+
await act(() => {
880+
root.render(
881+
<ComponentRenderingFlattenedChildren>
882+
<div />
883+
<div />
884+
</ComponentRenderingFlattenedChildren>,
885+
);
886+
});
887+
}).toErrorDev([]);
888+
});
889+
871890
it('should escape keys', () => {
872891
const zero = <div key="1" />;
873892
const one = <div key="1=::=2" />;

packages/react/src/jsx/ReactJSXElement.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ export function createElement(type, config, children) {
953953
}
954954

955955
export function cloneAndReplaceKey(oldElement, newKey) {
956-
return ReactElement(
956+
const clonedElement = ReactElement(
957957
oldElement.type,
958958
newKey,
959959
// When enableRefAsProp is on, this argument is ignored. This check only
@@ -966,6 +966,11 @@ export function cloneAndReplaceKey(oldElement, newKey) {
966966
__DEV__ && enableOwnerStacks ? oldElement._debugStack : undefined,
967967
__DEV__ && enableOwnerStacks ? oldElement._debugTask : undefined,
968968
);
969+
if (__DEV__) {
970+
// The cloned element should inherit the original element's key validation.
971+
clonedElement._store.validated = oldElement._store.validated;
972+
}
973+
return clonedElement;
969974
}
970975

971976
/**

0 commit comments

Comments
 (0)