Skip to content

Warning: Encountered two children with the same key

Error message

                Encountered two children with the same key, `X`. Keys must be unique.
              

Quick summary

Two or more list items have the same key value, which prevents React from correctly identifying which items changed.

Why this happens

  • Data items share the same ID (data integrity issue)

  • Using a non-unique field as the key (e.g., a category name)

  • Concatenating lists without ensuring key uniqueness

Minimal example

✗ Broken code
{items.map(item => (
  <li key={item.category}>{item.name}</li>
))}
// Multiple items can have the same category!
✓ Fixed code
{items.map(item => (
  <li key={item.id}>{item.name}</li>
))}
// Use unique id instead of category

Using a unique id instead of a shared category ensures every key is unique, preventing React from confusing items.

How to diagnose

  • Log all keys to check for duplicates

  • Verify the data source has unique IDs

  • Check if multiple lists are rendered in the same parent

How to fix

  • Ensure each item has a truly unique key

  • Prefix keys: key={${listName}-${item.id}}

  • Fix the data source to provide unique IDs

How to prevent

  • Always use unique IDs from the database as keys

  • When combining lists, prefix keys to ensure uniqueness

Related Resources

Related Glossary

Related Lessons

← Back to language