Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

React warns that each child in a list should have a unique key prop

ErrorWarning: 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.

  • Applies to React 19, Next.js 15, Next.js 16
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 -40
  2. 2Match what you see, then jump to its fix

    1. 1The element returned from map() is a plain tag or component with no key attribute.The mapped element has no keyFix
    2. 2You wrote key inside a component's own JSX, but the warning stays.The key is on an element inside the child componentFix
    3. 3The map() callback returns <>...</> with several children.Each item renders a short fragmentFix
    4. 4No 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

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 dev

Confirmed by react.dev

Did this fix it?

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 dev

Confirmed by react.dev

Did this fix it?

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 dev

Confirmed by react.dev

Did this fix it?

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/null

2Fix

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 dev

Confirmed by react.dev

Did this fix it?

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:

Related fixes

Web 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 fix

Web 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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.