Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

React: Each child in a list should have a unique key prop

Keys tell React which item is which between renders. Use stable ids, avoid index keys and put the key on the mapped element, not on a wrapper.

~5 min readReact
#react#keys#reconciliation#rendering
Bold white headline reading UNIQUE KEY beside a cyan React list rail with one amber duplicate node.

Introduction & Quick Summary

React prints a console warning that stops teams from shipping clean builds:

code
Warning: Each child in a list should have a unique "key" prop. Check the render method of `TodoList`. See https://react.dev/link/warning-keys for more information.

Quick answer: an array of elements rendered by map() needs a stable, unique key on each sibling. React uses that key to match elements between renders; without it, the reconciler falls back to position and warns.

tsx
{todos.map((todo) => ( <li key={todo.id}>{todo.title}</li> ))}

The warning is about identity, not formatting. A key taken from a database id preserves component state across reorders; an array index keeps state attached to a slot instead of an item. This guide explains how keys drive reconciliation, why index keys corrupt state, how keys work with fragments and nested lists, and when the duplicate-key warning appears.

Core Mechanics, Signatures & Reproducible Setup

key is a special prop that React reads before rendering. It is stored on the element, compared with the previous render, and never forwarded to the component.

tsx
type Todo = { id: string; title: string; done: boolean }; function TodoList({ todos }: { todos: Todo[] }) { return ( <ul> {todos.map((todo) => ( <li key={todo.id}>{todo.title}</li> ))} </ul> ); }

Reproduce the warning:

tsx
function Bad({ items }: { items: string[] }) { return ( <ul> {items.map((item) => ( <li>{item}</li> // no key ))} </ul> ); }

React reports the warning once per list per render pass. In React 18 and earlier it appears in the console; React 19 still warns but no longer prints the link text inside the message in every build, and a list with no keys at all in a StrictMode dev build can surface twice because effects run twice.

Render shapeNeeds keysWhy
items.map(...) returning one elementyesarray of siblings
literal siblings <A /><B />nostatic positions are stable
items.map(...) returning a fragmentyes, on the fragmentfragments are siblings too
nested map inside a mapyes, on botheach array is its own sibling list
Array.from({ length: n })yesan array is rendered
conditional {flag && <X />}nosingle child, position stable
Key sourceStable across reorderSafe for stateful children
database idyesyes
generated uuid stored in the itemyesyes
array indexnono
crypto.randomUUID() in renderno, changes every renderremounts every render
title or email stringuntil it is editedbreaks when edited
composite id + variantyesyes, lets variants remount

Key Comparison Table

Warning or symptomCauseFix
Each child in a list should have a unique "key"element returned from map has no keyadd a stable key
Encountered two children with the same keyduplicate ids, or index plus id collisionmake keys unique among siblings
Input loses focus or value after reorderindex key moved with the slotuse an item id
Component state jumps to the wrong rowindex key, or keys reused across listskey by identity
key is undefined inside the childreading props.keypass the value as a normal prop
List re-mounts on every keystrokekey generated during rendergenerate the id once, store it
Warning only in dev, never in buildReact strips the warning in productionthe bug is real, do not ignore it
PatternCorrectWrong
Key on list item<li key={todo.id}>key on the wrapper outside the map
Key on component<Row key={todo.id} todo={todo} /><Row todo={todo}><div key={todo.id} /></Row>
Key for fragments<Fragment key={id}><> with children needing identity
Key for nested listsouter key={group.id}, inner key={item.id}same id namespace for both
Key for placeholderskey={"skeleton-" + i} for a static countkey={Math.random()}

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

1. map without a key and the wrapper mistake

tsx
// wrong: key on the parent, not the item <ul> <div key="list"> {items.map((item) => <li>{item}</li>)} </div> </ul> // right <ul> {items.map((item) => <li key={item.id}>{item.label}</li>)} </ul>

The key must be on the element produced by the iterator callback, and it must be unique among its siblings, not globally. When a <div> wraps the list, the <li> elements still form their own sibling list and still need keys.

tsx
function Row({ item }: { item: Row }) { return <li>{item.label}</li>; } {rows.map((row) => <Row key={row.id} item={row} />)}

2. Duplicate keys

tsx
const groups = [ { id: 1, name: "A" }, { id: 1, name: "B" }, // duplicate from a bad join ]; {groups.map((g) => <li key={g.id}>{g.name}</li>)} // Warning: Encountered two children with the same key, `1`.

Duplicate keys make React reuse one subtree for both entries, so the second row can render the first row's state. Composite keys fix the case where one list intentionally holds two kinds of item:

tsx
{cells.map((c) => <Cell key={`${c.row}:${c.col}`} cell={c} />)}

Values are coerced to strings: key={1} and key={"1"} collide.

3. Fragments: the invisible sibling

tsx
import { Fragment } from "react"; {rows.map((row) => ( <Fragment key={row.id}> <dt>{row.term}</dt> <dd>{row.definition}</dd> </Fragment> ))}

Short syntax <>...</> cannot take a key. When a map returns several siblings, use Fragment with a key, or return a wrapper element that takes one.

4. Index keys and state loss

tsx
function Reorderable() { const [names, setNames] = useState(["Ada", "Grace", "Linus"]); return ( <ul> {names.map((name, i) => ( <li key={i}> {name} <input placeholder="note" /> </li> ))} </ul> ); }

Move "Ada" to the end and every <input> keeps the text that was typed in its position, so "Ada" appears to own the note that belonged to "Linus". With key={name} the components are matched to items and the notes follow their rows. Index keys are acceptable only for lists that never reorder, never filter and never insert in the middle.

5. Keys are not props

tsx
function Item(props: { id: string; label: string }) { console.log(props.id); // available if passed explicitly // props.key does not exist return <span>{props.label}</span>; } <Item key={item.id} id={item.id} label={item.label} />

React consumes key (and ref in React 18 and earlier) before the component sees props. If a child needs the id, pass it again under another name; the warning that follows "key is not a prop" is usually a console.log returning undefined.

6. Generated ids and keys in async data

tsx
// wrong: a new id every render remounts every row {items.map((item) => <Row key={crypto.randomUUID()} item={item} />)} // right: generate once when the item is created const [items, setItems] = useState(() => initial.map((i) => ({ ...i, key: crypto.randomUUID() })) );

Keys must be deterministic for a given item. A random key destroys and recreates the subtree on each render, which resets inputs, aborts animations and reruns effects. Use the JSON formatter to check that the API payload actually carries a unique id, and the diff checker to compare two rendered trees when a reorder bug is hard to see.

Common Compilation/Runtime Mistakes & Version Gotchas

  • Adding keys only where the warning points. React reports the component that rendered the list, not the inner list. A warning in Table may come from a map three levels down.
  • Suppressing the warning. There is no supported way to disable it, and silencing the console hides real state bugs rather than fixing them.
  • Assuming keys are required for correctness everywhere. They are required for arrays only. Static JSX does not need them, and adding them there is noise.
  • Using key to force a re-mount of a single child. This works, but it also destroys state and effects, so an explicit useEffect dependency is usually clearer.
  • Mixing keyed and unkeyed children. A list where some entries come from map and others are literal is still one sibling list; the literal children do not need keys, but the mapped ones do.
  • React 19 changes. ref is now a normal prop, but key is still reserved; the warning text and the link changed, the rule did not. React 19 also reports duplicate keys more consistently in production builds.
  • Server Components. Keys work the same way in Server Components, and a list rendered on the server that lacks keys still warns during the server render, so the warning can appear in the terminal rather than the browser.

Keys are the cheapest correctness tool React gives you: one stable string per item, chosen from data rather than position, and list bugs, focus loss and state drift mostly disappear.

Official documentation: Rendering lists and the Fragment reference.

Official references

primary documentation

The React documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit React work — they run entirely in this browser tab, so nothing you paste leaves your device.

All React guides

More in React

All React guides

The guides filed next to this one in React, in library order.