Skip to content
>_VibeUnstuck

Each child in a list should have a unique "key" prop

Last verified:

The fix

Each child in a list should have a unique "key" prop. Put a stable id on the element returned from map. Do not use Math.random(), and do not use the array index when the list can reorder.

  1. 1

    Put the key on the element map returns

    Use a field that is unique in this list and stays the same for that row: an id from the database, not the label text if two rows can share it. The key goes on the outer element in the callback.

    {items.map((item) => (
      <li key={item.id}>{item.name}</li>
    ))}
  2. 2

    Stop generating a key during render

    Delete key={Math.random()}, key={Date.now()}, and key={crypto.randomUUID()}. Those change every render. If the data has no id, derive one when the row is created and store it on the object. Do not add a key by wrapping the array in a fragment and keying the fragment's child instead of the array entry.

  3. 3

    Use the index only for a list that never changes order

    A static menu that is not sorted, filtered, or spliced can use key={index}. A todo list, a search result, or anything with state inside the row cannot. Index keys reuse the wrong state after a reorder. Prefer the id.

  4. 4

    Key a component, not a tag inside it

    Write <Row key={item.id} item={item} /> in the map. Adding key on a <div> inside Row does not identify the array entry. Do not pass key as a normal prop named key and read props.key. It will be undefined.

Why it happens

React prints Each child in a list should have a unique "key" prop when an array of elements is rendered and at least one of those elements has no key. The usual source is items.map(...) returning <li> or a component with no key. React uses the key to match a row to the same component instance on the next render. Without it, state inside the row sticks to the index, so a deleted or reordered row shows the wrong data.

The key belongs on the element in the array, the one the map callback returns. A key inside that child, on a nested <div>, does not count. key is not passed as a prop. Math.random() or crypto.randomUUID() during render makes a new key every time, so React throws the row away and mounts it again. The index is a stable key only when the list never reorders, filters, or inserts in the middle. A missing .map target is a different crash: Cannot read properties of undefined (reading 'map').

The next error you'll probably hit

Once this is fixed, the usual follow-up is Cannot read properties of undefined (reading 'map'). The value before .map is undefined or null, so there is no array to walk. Chrome names the property in parentheses. The usual generated case is rendering a fetch result before it is an array.

Prompt to paste back into your AI tool

Paste this above the error.

React warns: Each child in a list should have a unique "key" prop. The component named in the warning is <component>. The list is:
<paste the map>

Do not set key to Math.random(), Date.now(), or crypto.randomUUID(). Do not use the array index if this list can reorder, filter, or insert. Do not read props.key inside the child. Do not silence the warning by rendering the list as a string.

1. Put key on the element the map callback returns, using a stable id from the data.
2. If there is no id, say so, and add one when the row is created. Do not invent a random key at render.
3. If the crash is Cannot read properties of undefined (reading 'map'), stop. That is a different bug.
4. Change only this list.

Common questions

+The key is on a div inside the child and React still says each child should have a unique "key" prop. Why?

The key has to be on the element in the array, where map returns it. A key inside the child identifies something else. Move it to <Row key={item.id} />.

+Can I use the array index as the key?

Only when this list is static: no sorting, filtering, or inserting in the middle, and the row has no state of its own. Otherwise the index reuses the wrong row's state. Use the id.

+Two rows share an id and the warning is gone, but the UI is wrong. What happened?

React treated them as the same row. Keys must be unique among siblings. A duplicate id is not a fix for the warning.

Related errors