Error library / this error
How to fix it
Warning: Each child in a list should have a unique "key" prop.
What it means
React needs a stable identity for each item in a rendered list so it can update the right one.
Common causes
- A .map() returns elements without a key.
- The key is the array index and the list reorders, or two items share a key.
- The key is on a wrapper inside the component instead of on the element returned from map.
How to fix it
1. Use a stable id
Prefer a database id over anything computed at render time.
{users.map((u) => <UserRow key={u.id} user={u} />)}
2. Put the key on the outermost element returned
For fragments, use the long form.
{rows.map((r) => (
<React.Fragment key={r.id}>...</React.Fragment>
))}
3. Avoid random keys
Math.random() as a key remounts every item on every render.
Catch it the moment it happens
ErrorBell shows a dot on the page when this error occurs and takes you to the failing line, so you do not have to hunt through the console. Add it to Chrome, it is free.