-
Couldn't load subscription status.
- Fork 29.7k
Description
I mention this in additional context at the bottom, but just to be clear up top: I can not reproduce this bug using [email protected] itself. The warning only appears when I use next.js (which uses [email protected])
Link to the code that reproduces this issue
https://github.com/esseb/next-react-key-bug
To Reproduce
- Create a component with nested mapping where the key prop of the child in the first mapping is after a prop spread operator
- Example code below, or in the linked repository
- Open the "Source" tab in the browser's devtools
- Reload the page
Example code
const categories = [
{
name: "a",
subcategories: ["a0", "a1", "a2"]
},
{
name: "b",
subcategories: ["b0", "b1", "b2"]
},
{
name: "c",
subcategories: ["c0", "c1", "c2"]
}
]
export default function Home() {
return (
<div>
{categories.map((category, categoryIndex) => {
return (
<button
// The warning disappears if the key is before the spread operator
// key={categoryIndex}
{...{
className: 'example',
}}
key={categoryIndex}
>
<span>{category.name}</span>
<div>
{category.subcategories.map((subcategory, subcategoryIndex) => (
<span key={subcategoryIndex}>{subcategory}</span>
))}
</div>
</button>
);
})}
</div>
);
}Current vs. Expected behavior
Expected result
No warning
Actual result
This warning appears
Warning: Each child in a list should have a unique "key" prop. See https://reactjs.org/link/warning-keys for more information.
Further information
The warning disappears if the key prop of the child in the first mapping is before the prop spread operator.
Verify canary release
- I verified that the issue exists in the latest Next.js canary release
Provide environment information
Before running npm install next@canary
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 22.6.0: Wed Jul 5 22:22:05 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T6000
Binaries:
Node: 19.0.0
npm: 9.6.3
Yarn: 1.22.19
pnpm: 7.29.0
Relevant Packages:
next: 13.4.19
eslint-config-next: N/A
react: 18.2.0
react-dom: 18.2.0
typescript: N/A
Next.js Config:
output: N/AAfter running npm install next@canary
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 22.6.0: Wed Jul 5 22:22:05 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T6000
Binaries:
Node: 19.0.0
npm: 9.6.3
Yarn: 1.22.19
pnpm: 7.29.0
Relevant Packages:
next: 13.5.1-canary.1
eslint-config-next: N/A
react: 18.2.0
react-dom: 18.2.0
typescript: N/A
Next.js Config:
output: N/AWhich area(s) are affected? (Select all that apply)
Not sure
Additional context
My reproduction repository contains two folders:
nextcreate-react-app
Both use [email protected] judging from the respective package-lock.json file. The same code is in both apps but the warning only appears in the next app, so I don't think the bug is in React itself.