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 — Fix Missing `key` Validation in `React.Children` by yungsters · Pull Request #29675 · react/react

Conversation

@yungsters

@yungsters yungsters commented May 30, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@vercel

vercel Bot commented May 30, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-compiler-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 31, 2024 0:58am
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@facebook-github-bot facebook-github-bot added the CLA Signed label May 30, 2024
@vercel
vercel Bot deployed to Preview May 30, 2024 23:02
View deployment
yungsters
yungsters commented May 30, 2024
View reviewed changes

@yungsters yungsters left a comment

Copy link
Copy Markdown
Contributor Author

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.

Oh wait, this does not include my most recent changes. Silly me…

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread packages/react/src/__tests__/ReactChildren-test.js
return (
<div>
{React.Children.map(children, child => (
<div />

@yungsters yungsters May 30, 2024

Copy link
Copy Markdown
Contributor Author

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.

React actually does not care if mappedChild is missing a key.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread packages/react/src/__tests__/ReactChildren-test.js
await act(() => {
root.render(
<ComponentRenderingMappedChildren>
{[<div />]}

@yungsters yungsters May 30, 2024

Copy link
Copy Markdown
Contributor Author

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.

However, React does care if the supplied child is missing a key (and is in a list).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread packages/react/src/__tests__/ReactChildren-test.js
Comment on lines +914 to +915
<div />
<div />

@yungsters yungsters May 30, 2024

Copy link
Copy Markdown
Contributor Author

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.

React is content with static child, even if mappedChild does not have a key.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread packages/react/src/__tests__/ReactChildren-test.js
function ComponentRenderingClonedChildren({children}) {
return (
<div>
{React.Children.map(children, child => React.cloneElement(child))}

@yungsters yungsters May 30, 2024

Copy link
Copy Markdown
Contributor Author

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.

This is the use case that I originally sought to fix.

I suppose this is now more or less the same as the test case above (that simply returns <div />), but maybe still worth keeping as a test case?

Seeking input on whether to keep this and the test case below, or to delete them.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread packages/react/src/__tests__/ReactChildren-test.js
});

it('does not warn for flattened positional children', async () => {
it('does not warn for flattened static children without keys', async () => {

@yungsters yungsters May 30, 2024

Copy link
Copy Markdown
Contributor Author

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 realized while reading more code that we refer to these as "static" children, so this is just a rename.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@yungsters
Fix Missing key Validation in React.Children
1a6212d
@yungsters
yungsters force-pushed the key-warnings branch from 8fe1731 to 1a6212d Compare May 30, 2024 23:04
@yungsters
yungsters requested a review from sebmarkbage May 30, 2024 23:05
yungsters
yungsters commented May 30, 2024
View reviewed changes
Comment thread packages/react/src/jsx/ReactJSXElement.js Outdated

export function cloneAndReplaceKey(oldElement, newKey) {
const clonedElement = ReactElement(
return ReactElement(

@yungsters yungsters May 30, 2024

Copy link
Copy Markdown
Contributor Author

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.

This is a revert of #29662, which is no longer necessary.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@vercel
vercel Bot deployed to Preview May 30, 2024 23:07
View deployment
@react-sizebot

react-sizebot commented May 30, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Comparing: 9710853...5d26f17

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.js = 6.66 kB 6.66 kB = 1.82 kB 1.82 kB
oss-stable/react-dom/cjs/react-dom-client.production.js = 496.39 kB 496.39 kB = 88.84 kB 88.84 kB
oss-experimental/react-dom/cjs/react-dom.production.js = 6.67 kB 6.67 kB = 1.83 kB 1.83 kB
oss-experimental/react-dom/cjs/react-dom-client.production.js = 501.21 kB 501.21 kB = 89.54 kB 89.54 kB
facebook-www/ReactDOM-prod.classic.js = 593.88 kB 593.88 kB = 104.46 kB 104.46 kB
facebook-www/ReactDOM-prod.modern.js = 570.26 kB 570.26 kB = 100.87 kB 100.87 kB
test_utils/ReactAllWarnings.js Deleted 63.65 kB 0.00 kB Deleted 15.90 kB 0.00 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
oss-experimental/react/cjs/react.react-server.development.js +0.76% 58.59 kB 59.03 kB +0.74% 16.69 kB 16.81 kB
oss-stable-semver/react/cjs/react.react-server.development.js +0.64% 69.70 kB 70.14 kB +0.65% 19.52 kB 19.65 kB
oss-stable/react/cjs/react.react-server.development.js +0.64% 69.72 kB 70.16 kB +0.64% 19.55 kB 19.67 kB
oss-experimental/react/cjs/react.development.js +0.56% 78.88 kB 79.32 kB +0.58% 21.71 kB 21.84 kB
oss-stable-semver/react/cjs/react.development.js +0.47% 94.09 kB 94.54 kB +0.48% 25.81 kB 25.94 kB
oss-stable/react/cjs/react.development.js +0.47% 94.12 kB 94.56 kB +0.48% 25.84 kB 25.97 kB
facebook-react-native/react/cjs/React-dev.js +0.42% 105.05 kB 105.50 kB +0.49% 28.22 kB 28.36 kB
facebook-www/React-dev.modern.js +0.38% 116.10 kB 116.55 kB +0.44% 30.13 kB 30.27 kB
facebook-www/React-dev.classic.js +0.38% 116.59 kB 117.04 kB +0.42% 30.23 kB 30.36 kB
test_utils/ReactAllWarnings.js Deleted 63.65 kB 0.00 kB Deleted 15.90 kB 0.00 kB

Generated by 🚫 dangerJS against 5d26f17

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@yungsters
Fix Flow errors
2897d38
@vercel
vercel Bot deployed to Preview May 30, 2024 23:15
View deployment
@yungsters
Prettier...
63f6541
@vercel
vercel Bot deployed to Preview May 30, 2024 23:18
View deployment
sebmarkbage
sebmarkbage reviewed May 31, 2024
View reviewed changes
Comment thread packages/react/src/jsx/ReactJSXElement.js
);
if (__DEV__) {
// The cloned element should inherit the original element's key validation.
clonedElement._store.validated = oldElement._store.validated;

@sebmarkbage sebmarkbage May 31, 2024

Copy link
Copy Markdown
Contributor

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.

If the original element was validated === 2 because it came from Flight or another Children helper, then was passed through map again, then it turns into validated === 0 but since it does have a key now it wouldn't issue an error even though it should've.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

yungsters reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@yungsters yungsters May 31, 2024

Copy link
Copy Markdown
Contributor Author

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.

When I started tackling this, I was also considering copying this logic to React.cloneElement.

I'll revert this change for now, but I mention it to ask whether you think React.cloneElement should also persist the original element's _store.validated (and whether that is affected by a new key being supplied to React.cloneElement).

If you think there's something to do there, I'd be happy to follow up.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
sebmarkbage
sebmarkbage reviewed May 31, 2024
View reviewed changes
Comment thread packages/react/src/ReactChildren.js
// print the warning later.
// If `child` was an element without a `key`, we need to validate if
// it should have had a `key`, before assigning one to `mappedChild`.
// $FlowFixMe[incompatible-type] Flow incorrectly thinks React.Portal doesn't have a key

@sebmarkbage sebmarkbage May 31, 2024

Copy link
Copy Markdown
Contributor

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.

It is weird that React.Children doesn't operate on portals. They don't get assigned keys.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@yungsters yungsters May 31, 2024

Copy link
Copy Markdown
Contributor Author

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.

To be honest, I copied this from the $FlowFixMe comments in the lines above. 😅

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@sebmarkbage sebmarkbage May 31, 2024

Copy link
Copy Markdown
Contributor

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 know but I found it interesting that 1) we didn't refine the type above 2) that we're checking isValidElement at all.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
sebmarkbage
sebmarkbage approved these changes May 31, 2024
View reviewed changes

@sebmarkbage sebmarkbage left a comment

Copy link
Copy Markdown
Contributor

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 think the revert is actually not correct now. We should probably keep that to transfer any unissued "forced" warning to happen later. Left a comment.

The other changes looks right though. Interesting that the root gets assigned a key.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

yungsters reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@yungsters
Restore cloneAndReplaceKey changes
5d26f17
@yungsters

yungsters commented May 31, 2024

Copy link
Copy Markdown
Contributor Author

Thanks for the quick review!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vercel
vercel Bot deployed to Preview May 31, 2024 00:58
View deployment
sebmarkbage
sebmarkbage reviewed May 31, 2024
View reviewed changes
Comment thread packages/react/src/ReactChildren.js
if (
nameSoFar !== '' &&
child != null &&
isValidElement(child) &&

@sebmarkbage sebmarkbage May 31, 2024

Copy link
Copy Markdown
Contributor

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.

Now that I'm looking closer, it seems like we're extracting a key from Portals with getElementKey.

I believe that this check should really check if is element or is portal.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@sebmarkbage sebmarkbage May 31, 2024

Copy link
Copy Markdown
Contributor

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 guess Portals don't have stores and they don't run the validation in ChildFiber so this is not necessary right now, and it's probably not worth adding key validation for portals after the fact.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@yungsters
yungsters merged commit 8fd963a into react:main May 31, 2024
@yungsters
yungsters deleted the key-warnings branch May 31, 2024 01:02
github-actions Bot pushed a commit that referenced this pull request May 31, 2024
@yungsters
Fix Missing key Validation in React.Children (#29675)
6de1973
## 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.
github-actions Bot pushed a commit that referenced this pull request May 31, 2024
@yungsters
Fix Missing key Validation in React.Children (#29675)
e2fbd8f
## 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)
@eps1lon eps1lon mentioned this pull request Jun 4, 2024
Update React from f994737d14 to 1df34bdf62 (19.0.0-rc.0) vercel/next.js#66533
Merged
hanzo-dev pushed a commit to hanzoai/react that referenced this pull request Sep 9, 2026
@yungsters
Fix Missing key Validation in React.Children (react#29675)
3d2e8a5
## Summary

In react#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
react#29662.

## How did you test this change?

```
$ yarn test ReactChildren-test.js
```

This branch was successfully deployed

1 active deployment
Preview — 5d26f171 Deployed May 31, 2024 by vercel[bot]
View deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

1 more reviewer
@sebmarkbage sebmarkbage sebmarkbage approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

CLA Signed

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@yungsters @react-sizebot @sebmarkbage @facebook-github-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — 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.