Skip to content
engineering workbench.by StudyPort Academy
All errors
logic · DEBUG LIBRARY

Each child in a list should have a unique key prop

Locate the failed assumption, reproduce it, then repair the cause.

What actually caused it

React is reconciling an of children and has no stable identity for each item. Without a key, it falls back to the index. That works only while the list never inserts, deletes, or reorders. After a reorder, React updates the component that stayed in that slot, so state such as a text input stays with the position instead of the item. Duplicate keys are worse: React warns, and the matching becomes ambiguous, so nodes can be reused in the wrong order or appear to duplicate.

The warning shows up in development when you map an and return elements with no key, or with a key that is not unique among siblings. The key is not a prop your component reads. It is an instruction to the reconciler.

Minimal reproduction

Index keys on a list that reorders

items.map((item, index) => (
  <Row key={index} item={item} />
));

Sorting the leaves each row’s state in the old slot.

The fix

Pass a key that identifies the item among its siblings for the lifetime of that list, usually an id from your data. Do not use the index when the order can change, and do not use Math.random() because a new key every render remounts the row and throws its state away.

If two rows can share an id, fix the data. Keys only need to be unique among siblings, not across the whole app. After the change, reorder the list and confirm that each row’s input or selection stays with the same item.

A stable id from the item

items.map((item) => (
  <Row key={item.id} item={item} />
));

The id stays with the item when the order changes.