Fix guides · Next.js, React, Cursor, Claude Code, Lovable, Bolt, v0 ·
Fix React "Each child in a list should have a unique key" warning
Warning: Each child in a list should have a unique "key" prop.
React warns when array items rendered with .map() lack a stable unique key. Without a key, React can't track which item is which across renders, causing wrong DOM updates, lost input state, or unnecessary re-renders when the list is sorted, filtered, or items inserted/deleted. The fix is to add key={item.id} (or another stable identifier from the data) to each mapped element. Never use Math.random() as a key, and prefer a stable id over the array index.
Common causes – and how to tell which one you have
1. Missing key on mapped elements
How to tell: The console shows "Each child in a list should have a unique \"key\" prop." and points at a .map() that renders <li>, <div>, or a component without a key prop.
Fix: Add key={item.id} (a stable value from your data) to the root element inside each .map() iteration.
2. Index used as key
How to tell: The code uses key={index} or no key at all, and sorting/filtering the list produces wrong UI state or loses input focus.
Fix: Replace key={index} with key={item.id} where id is a stable identifier from the data. Only use index as a last resort for static, never-reordered lists.
3. Random or timestamp keys
How to tell: The code uses key={Math.random()} or key={Date.now()}, forcing React to re-create every item on every render.
Fix: Replace with key={item.id} from a stable data identifier. Random keys never match across renders.
How to fix it
- Open the component named in the warning (the map call's container).
- Find the .map() call that returns JSX for each item.
- Add a key prop to the root element inside the map: <li key={person.id}>...</li>.
- Make sure every key is a stable value from the data (an id, slug, or database primary key), not Math.random() or a timestamp.
- Prefer an explicit id over the array index; index as key causes bugs when items are reordered, inserted, or deleted.
- Remove the key warning from your app and reload to confirm the console is clean.
- If an item genuinely has no id, add one to your data source instead of inventing a key at render time.
Using Lovable, Bolt or v0?
In Lovable, Bolt or v0: "My app warns that list items need unique keys. Add key={item.id} to each mapped element inside every .map(), using a stable id from the data — not Math.random() or the index — and confirm the warning is gone."
Using Cursor, Claude Code or Windsurf?
Ask Cursor or Claude Code to find every .map() that renders JSX, add key={item.id} (stable data id) to the root element of each item, avoid index/Random keys, and verify the warning disappears in the browser console.
The “fix” to avoid
Do not suppress the warning with a dummy key, use index keys on lists that reorder, or use Math.random()/Date.now() keys. Random and timestamp keys never match across renders and force React to recreate the DOM every render; index keys stay stable per position but misattribute state when items move. Use a stable id instead.
Copy-paste fix prompt for your AI coding tool
Paste this into Cursor, Claude Code, Lovable or Bolt:
My React app warns: "Each child in a list should have a unique \"key\" prop." Find every .map() call that renders JSX items and add key={item.id} (or another stable identifier from the data) to the root element of each mapped item. Do not use Math.random() or Date.now() as a key, and prefer a real id over the array index. Show me which element needed the key, what value you used, and confirm the warning disappears in the browser console.
Questions people ask
Can I use the array index as key?
Only for lists that never sort, filter, insert, or delete items. For any dynamic list, use a stable data id instead — index keys cause subtle state bugs.
Does key need to be globally unique?
No — only unique among siblings in the same list. Keys don't need to be globally unique across the app, and components don't receive key as a prop.
What if my data has no id?
Add one to your data source (database primary key, slug, or generated stable identifier) rather than computing a key at render time.