Error Encyclopedia

React Missing Key Prop Warning

Fix 'Each child in a list should have a unique key' warning in React. Learn why keys are important and how to choose good key values.

What Does This Error Mean?

This React warning means you are rendering a list of elements without providing a key prop, or using array indices as keys. Keys help React identify which items have changed, been added, or been removed in a list.

Common Causes

1

Missing key prop on mapped elements

2

Using array index as key (ok for static lists, bad for dynamic)

3

Duplicate key values when items share the same identifier

4

Key prop set on a Fragment instead of the child element

5

Using Math.random() as key (causes unnecessary remounting)

How to Fix It

Add unique keys from data

Use a unique identifier from your data as the key.

const users = [
  { id: 1, name: "Alice" },
  { id: 2, name: "Bob" }
]

// ✅ Use unique id from data
return (
  <ul>
    {users.map(user => (
      <li key={user.id}>{user.name}</li>
    ))}
  </ul>
)

Use index as last resort

Only use array index for static lists that don't change order.

// ✅ Static list: index is fine
const colors = ["red", "green", "blue"]
return (
  <div>
    {colors.map((color, i) => (
      <span key={i}>{color}</span>
    ))}
  </div>
)

// ❌ Dynamic list: index causes bugs
// When items are added/removed/reordered, indices shift

Generate stable IDs if none exist

Create stable identifiers using a combination of properties.

const items = [
  { name: "Apple", type: "fruit" },
  { name: "Carrot", type: "vegetable" }
]

// Create composite key if no unique id
return items.map(item => (
  <li key={`${item.type}-${item.name}`}>
    {item.name}
  </li>
))

Related Tools

Use these tools to debug and fix this error:

Related Errors

Other common errors in this category: