-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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
Fix key
Warning for Flattened Positional Children
#29662
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Comparing: c2b45ef...49267d4 Critical size changesIncludes critical production bundles, as well as any change greater than 2%:
Significant size changesIncludes any change greater than 0.2%: Expand to show
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on the changes made in #29088 this makes sense.
it('does not warn for flattened positional children', async () => { | ||
function ComponentRenderingFlattenedChildren({children}) { | ||
return <div>{React.Children.toArray(children)}</div>; | ||
} | ||
|
||
const container = document.createElement('div'); | ||
const root = ReactDOMClient.createRoot(container); | ||
await expect(async () => { | ||
await act(() => { | ||
root.render( | ||
<ComponentRenderingFlattenedChildren> | ||
<div /> | ||
<div /> | ||
</ComponentRenderingFlattenedChildren>, | ||
); | ||
}); | ||
}).toErrorDev([]); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add another example where it does still correctly warn if an array was used without keys?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should have test for this already
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Surprisingly, we don't for the simple case. (We do for iterables of children.)
@@ -966,6 +966,11 @@ export function cloneAndReplaceKey(oldElement, newKey) { | |||
__DEV__ && enableOwnerStacks ? oldElement._debugStack : undefined, | |||
__DEV__ && enableOwnerStacks ? oldElement._debugTask : undefined, | |||
); | |||
if (__DEV__) { | |||
// The cloned element should inherit the original element's key validation. | |||
clonedElement._store.validated = oldElement._store.validated; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to check if oldElement._store
exists? We do so in other calls.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed that, too.
I don't think it's necessary because we always define it in __DEV__
, and we also assume it exists shortly after this method call (when we conditionally set validated
to 2).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps the other call sites check for _store
because they might be receiving malformed elements (e.g. constructed by third party libraries or something).
But here, we are creating clonedElement
.
## Summary #29088 introduced a regression triggering this warning when rendering flattened positional children: > Each child in a list should have a unique "key" prop. The specific scenario that triggers this is when rendering multiple positional children (which do not require unique `key` props) after flattening them with one of the `React.Children` utilities (e.g. `React.Children.toArray`). The refactored logic in `React.Children` incorrectly drops the `element._store.validated` property in `__DEV__`. This diff fixes the bug and introduces a unit test to prevent future regressions. ## How did you test this change? ``` $ yarn test ReactChildren-test.js ``` DiffTrain build for commit 72644ef.
## Summary #29088 introduced a regression triggering this warning when rendering flattened positional children: > Each child in a list should have a unique "key" prop. The specific scenario that triggers this is when rendering multiple positional children (which do not require unique `key` props) after flattening them with one of the `React.Children` utilities (e.g. `React.Children.toArray`). The refactored logic in `React.Children` incorrectly drops the `element._store.validated` property in `__DEV__`. This diff fixes the bug and introduces a unit test to prevent future regressions. ## How did you test this change? ``` $ yarn test ReactChildren-test.js ``` DiffTrain build for [72644ef](72644ef)
## Summary In #29088, the validation logic for `React.Children` inspected whether `mappedChild` — the return value of the map callback — has a valid `key`. However, this deviates from existing behavior which only warns if the original `child` is missing a required `key`. This fixes false positive `key` validation warnings when using `React.Children`, by validating the original `child` instead of `mappedChild`. This is a more general fix that expands upon my previous fix in #29662. ## How did you test this change? ``` $ yarn test ReactChildren-test.js ```
## Summary In #29088, the validation logic for `React.Children` inspected whether `mappedChild` — the return value of the map callback — has a valid `key`. However, this deviates from existing behavior which only warns if the original `child` is missing a required `key`. This fixes false positive `key` validation warnings when using `React.Children`, by validating the original `child` instead of `mappedChild`. This is a more general fix that expands upon my previous fix in #29662. ## How did you test this change? ``` $ yarn test ReactChildren-test.js ``` DiffTrain build for commit 8fd963a.
## Summary In #29088, the validation logic for `React.Children` inspected whether `mappedChild` — the return value of the map callback — has a valid `key`. However, this deviates from existing behavior which only warns if the original `child` is missing a required `key`. This fixes false positive `key` validation warnings when using `React.Children`, by validating the original `child` instead of `mappedChild`. This is a more general fix that expands upon my previous fix in #29662. ## How did you test this change? ``` $ yarn test ReactChildren-test.js ``` DiffTrain build for [8fd963a](8fd963a)
Summary
#29088 introduced a regression triggering this warning when rendering flattened positional children:
The specific scenario that triggers this is when rendering multiple positional children (which do not require unique
key
props) after flattening them with one of theReact.Children
utilities (e.g.React.Children.toArray
).The refactored logic in
React.Children
incorrectly drops theelement._store.validated
property in__DEV__
. This diff fixes the bug and introduces a unit test to prevent future regressions.How did you test this change?