ErrorOracle
javascript

Warning: Each child in a list should have a unique 'key' pr…

Error message

Warning: Each child in a list should have a unique 'key' prop.

What broke

In React, when rendering a list of elements, each child must have a unique 'key' prop to help React identify which items have changed, been added, or removed. Without unique keys, React may not efficiently update the UI.

Why it broke

The warning occurs because the list items are being rendered without a unique identifier, which can lead to performance issues and incorrect rendering behavior. React relies on these keys to optimize rendering.

How to fix

To fix this, ensure that each element in the list has a unique 'key' prop. You can use an ID from your data or the index of the item in the array, but using a stable ID is preferred.

Code fix

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

Explained by ErrorOracle

Prevention tip

You can use an ID from your data or the index of the item in the array, but using a stable ID is preferred.

Was this helpful?

AI-generated explanation. Always verify fixes in your codebase.