Skip to content

Commit

Permalink
Fix key Warning for Flattened Positional Children (#29662)
Browse files Browse the repository at this point in the history
## 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.
  • Loading branch information
yungsters committed May 30, 2024
1 parent b0cfb9c commit ae612e6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @noflow
* @nolint
* @preventMunge
* @generated SignedSource<<a504442f74a5c97d4fa7e8c91e8156f4>>
* @generated SignedSource<<f3b415267f0bfb7665b4338695d8d314>>
*/

'use strict';
Expand All @@ -24,7 +24,7 @@ if (
}
var dynamicFlagsUntyped = require('ReactNativeInternalFeatureFlags');

var ReactVersion = '19.0.0-rc-86d4e19f';
var ReactVersion = '19.0.0-rc-37e13b86';

// Re-export dynamic flags from the internal module.
var dynamicFlags = dynamicFlagsUntyped; // We destructure each value before re-exporting to avoid a dynamic look-up on
Expand Down Expand Up @@ -1562,9 +1562,16 @@ function createElement(type, config, children) {
return ReactElement(type, key, ref, undefined, undefined, getOwner(), props);
}
function cloneAndReplaceKey(oldElement, newKey) {
return ReactElement(oldElement.type, newKey, // When enableRefAsProp is on, this argument is ignored. This check only
var clonedElement = ReactElement(oldElement.type, newKey, // When enableRefAsProp is on, this argument is ignored. This check only
// exists to avoid the `ref` access warning.
null , undefined, undefined, oldElement._owner, oldElement.props);

{
// The cloned element should inherit the original element's key validation.
clonedElement._store.validated = oldElement._store.validated;
}

return clonedElement;
}
/**
* Clone and return a new ReactElement using element as the starting point.
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
51dd09631ac0c7824fec55f38462846b6fe41d06
72644ef2f2ec7a274f79f6b32320d62757521329

0 comments on commit ae612e6

Please sign in to comment.