React warns that each child in a list should have a unique key prop
Warning: Each child in a list should have a unique "key" prop.Short answer
React wants a stable key on the element that sits directly inside a map() call, so it can match list items between renders. The warning appears when the key is missing, is placed on the wrong element, is attached to a short fragment, or is not unique or stable. Give each item an ID from your data and put it on the outermost mapped element.
Fix it fast
1Find out which cause you have
The warning names the component whose render created the list. Open it and find the map() call that returns JSX. Then check, for that one call, which of the four causes below matches.
grep -rnE "\.map\(" app src components 2>/dev/null | head -402Match what you see, then jump to its fix
- 1
The element returned from map() is a plain tag or component with no key attribute.The mapped element has no keyFix - 2
You wrote key inside a component's own JSX, but the warning stays.The key is on an element inside the child componentFix - 3
The map() callback returns <>...</> with several children.Each item renders a short fragmentFix - 4
No warning, or a warning about one item, but inputs inside list rows lose their text, or rows jump when the list is sorted or filtered.Keys are repeated, change on every render, or follow the array indexFix
- 1
What does the error look like?
Warning: Each child in a list should have a unique "key" prop.
Cause 1 of 4
The mapped element has no key
1Check
JSX elements directly inside a map() call always need keys. Look at what the arrow function returns.
{people.map(person =>
<li>{person.name}</li> // no key
)}2Fix
Use a value that already identifies the item, such as a database ID. For data you create in the browser, generate the ID once when the item is created, with crypto.randomUUID() or a counter.
{people.map(person =>
<li key={person.id}>{person.name}</li>
)}3Verify
Reload and check that the warning is gone from the browser console.
npm run devConfirmed by react.dev
Cause 2 of 4
The key is on an element inside the child component
1Check
The key must be on the element returned by map(), not on the root element inside the component it renders.
function Recipe({ id, name }) {
return <div key={id}>{name}</div> // too deep
}
recipes.map(r => <Recipe {...r} />)2Fix
Move the key to the place where the component is created in the list.
recipes.map(r => <Recipe {...r} key={r.id} />)3Verify
The warning no longer lists that component.
npm run devConfirmed by react.dev
Cause 3 of 4
Each item renders a short fragment
1Check
The shorthand fragment syntax cannot take a key.
people.map(p => (
<>
<h1>{p.name}</h1>
<p>{p.bio}</p>
</>
))2Fix
Import Fragment from react and write it out so it can carry the key.
import { Fragment } from 'react'
people.map(p => (
<Fragment key={p.id}>
<h1>{p.name}</h1>
<p>{p.bio}</p>
</Fragment>
))3Verify
Reload. The warning is gone and the DOM still shows a flat list of h1 and p elements.
npm run devConfirmed by react.dev
Cause 4 of 4
Keys are repeated, change on every render, or follow the array index
1Check
Keys must be unique among siblings and must not change. The array index is what React falls back to when no key is given, and it breaks when items are inserted, removed or reordered. A key made with Math.random() during render never matches between renders, so every row is rebuilt and user input is lost.
grep -rnE "key=\{(index|i|idx|Math\.random)" app src components 2>/dev/null2Fix
Use a stable ID from the data. Index keys are fine only for a list that never reorders, such as fixed lines of text. A key is not passed to the component as a prop, so pass the ID separately if the component needs it.
items.map(item => <Row key={item.id} id={item.id} label={item.label} />)3Verify
Type into one row's input, then reorder or filter the list. The text stays with its row.
npm run devConfirmed by react.dev
How do I stop it coming back?
- Make every list item carry an ID from the moment it is created.
- Never build a key during render with Math.random() or Date.now().
- Review list code with the React docs rule in mind: key goes on the outermost element returned by map().
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
ESLint says an element in a list is missing its key prop
react/jsx-key
react/jsx-key fires when an element returned from map, Array.from or an array literal has no key.
3 causes, each with a fixWeb apps
There is no tailwind.config.js after installing Tailwind 4 and HeroUI in Next.js
HeroUI components render unstyled
Nothing is broken: Tailwind CSS 4 is configured in CSS and no longer creates tailwind.config.js.
3 causes, each with a fixWeb apps
react-i18next shows the wrong plural form for a count, or prints the key instead of the text
t('item') prints the key item instead of a translated sentence
i18next picks a plural from the count option and looks for keys ending in _one, _other and so on.
4 causes, each with a fixWeb apps
useSession throws because there is no SessionProvider above it
[next-auth]: `useSession` must be wrapped in a <SessionProvider />
useSession reads the session from React Context, and SessionProvider is what creates that context.
3 causes, each with a fixWeb apps
next/link throws React.Children.only on its children
Error: React.Children.only expected to receive a single React element child.
React.Children.only accepts exactly one React element and throws for anything else, including an array with one item.
3 causes, each with a fixWeb apps
Hydration failed after adding next-themes dark mode
Console reports a server and client mismatch on the html element
next-themes changes the html element and knows the theme only in the browser, so server HTML and client HTML differ.
3 causes, each with a fix