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 `key` Warning for Flattened Positional Children by yungsters · Pull Request #29662 · react/react

Conversation

@yungsters

@yungsters yungsters commented May 30, 2024

Copy link
Copy Markdown
Contributor

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

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 30, 2024 2:23pm
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
@yungsters
yungsters requested a review from sebmarkbage May 30, 2024 04:21
@vercel
vercel Bot deployed to Preview May 30, 2024 04:23
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: c2b45ef...49267d4

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 +0.11% 1.82 kB 1.82 kB
oss-stable/react-dom/cjs/react-dom-client.production.js = 496.38 kB 496.38 kB = 88.84 kB 88.84 kB
oss-experimental/react-dom/cjs/react-dom.production.js = 6.67 kB 6.67 kB +0.16% 1.82 kB 1.83 kB
oss-experimental/react-dom/cjs/react-dom-client.production.js = 501.20 kB 501.20 kB = 89.53 kB 89.53 kB
facebook-www/ReactDOM-prod.classic.js = 593.81 kB 593.81 kB = 104.45 kB 104.45 kB
facebook-www/ReactDOM-prod.modern.js = 570.20 kB 570.20 kB = 100.85 kB 100.85 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.33% 58.40 kB 58.59 kB +0.32% 16.64 kB 16.69 kB
oss-stable-semver/react/cjs/react.react-server.development.js +0.28% 69.50 kB 69.70 kB +0.28% 19.47 kB 19.52 kB
oss-stable/react/cjs/react.react-server.development.js +0.28% 69.53 kB 69.72 kB +0.29% 19.49 kB 19.55 kB
oss-experimental/react/cjs/react.development.js +0.25% 78.69 kB 78.88 kB +0.24% 21.66 kB 21.71 kB
oss-stable-semver/react/cjs/react.development.js +0.21% 93.90 kB 94.09 kB +0.17% 25.77 kB 25.81 kB
oss-stable/react/cjs/react.development.js +0.21% 93.92 kB 94.12 kB +0.17% 25.80 kB 25.84 kB
test_utils/ReactAllWarnings.js Deleted 63.65 kB 0.00 kB Deleted 15.90 kB 0.00 kB

Generated by 🚫 dangerJS against 49267d4

All reactions

Sorry, something went wrong.

Uh oh!

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

@yungsters
Fix key Warning for Flattened Positional Children
6b46ad9
@yungsters
yungsters force-pushed the key-validation branch from 3165a07 to 6b46ad9 Compare May 30, 2024 04:36
@vercel
vercel Bot deployed to Preview May 30, 2024 04:39
View deployment
javache
javache approved these changes May 30, 2024
View reviewed changes

@javache javache 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.

Based on the changes made in #29088 this makes sense.

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 +871 to +888
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([]);
});

@javache javache May 30, 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.

Maybe add another example where it does still correctly warn if an array was used without keys?

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

@rickhanlonii rickhanlonii May 30, 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.

we should have test for this already

Sorry, something went wrong.

Uh oh!

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

All reactions

@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.

Surprisingly, we don't for the simple case. (We do for iterables of children.)

Sorry, something went wrong.

Uh oh!

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

All reactions
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;

@javache javache May 30, 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.

Do we need to check if oldElement._store exists? We do so in other calls.

Sorry, something went wrong.

Uh oh!

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

All reactions

@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 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).

Sorry, something went wrong.

Uh oh!

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

All reactions

@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.

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.

Sorry, something went wrong.

Uh oh!

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

All reactions

@smeng9 smeng9 Jan 13, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown

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.

Hi @yungsters, we have encountered issue that such keys does not exist.

Our use case is as follows, we use microfrontend where some part of code is built with production flag and loaded through https://github.com/Paciolan/remote-component and some other part of the code is used in development mode.

Production code in react does not have private variables like _store and when cloning it certainly has problem.

Please check for the existences of _store.

Sorry, something went wrong.

Uh oh!

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

All reactions

@yungsters yungsters Jan 18, 2025

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've submitted #32117.

Sorry, something went wrong.

Uh oh!

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

All reactions
@yungsters
Add test case for flattened children lists
49267d4
@vercel
vercel Bot deployed to Preview May 30, 2024 14:23
View deployment
@yungsters
yungsters merged commit 72644ef into react:main May 30, 2024
@yungsters
yungsters deleted the key-validation branch May 30, 2024 14:26
github-actions Bot pushed a commit that referenced this pull request May 30, 2024
@yungsters
Fix key Warning for Flattened Positional Children (#29662)
ae612e6
## 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.
github-actions Bot pushed a commit that referenced this pull request May 30, 2024
@yungsters
Fix key Warning for Flattened Positional Children (#29662)
4bebe8e
## 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)
@yungsters yungsters mentioned this pull request May 30, 2024
Fix Missing key Validation in React.Children #29675
Merged
yungsters added a commit that referenced this pull request May 31, 2024
@yungsters
Fix Missing key Validation in React.Children (#29675)
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
```
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
@smeng9 smeng9 mentioned this pull request Jan 13, 2025
fix(JSXElement): React19 regression: element._store may not be present in mixed dev/prod environment #32061
Closed
@yungsters yungsters mentioned this pull request Jan 18, 2025
Permit non-DEV Elements in React.Children w/ DEV #32117
Merged
hanzo-dev pushed a commit to hanzoai/react that referenced this pull request Sep 9, 2026
@yungsters
Fix key Warning for Flattened Positional Children (react#29662)
8e63197
## Summary

react#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
```
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 — 49267d4d Deployed May 30, 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

@javache javache javache approved these changes
@sebmarkbage sebmarkbage Awaiting requested review from sebmarkbage
+2 more reviewers
@rickhanlonii rickhanlonii rickhanlonii left review comments
@smeng9 smeng9 smeng9 left review comments
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.

6 participants

@yungsters @react-sizebot @javache @rickhanlonii @smeng9 @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.