Skip to content

Missing key prop in list rendering

Quick summary

You rendered a list without key props — React cannot track which items changed, leading to bugs when items are added, removed, or reordered.

Why this happens

  • You used .map() to render a list but did not pass a key prop to each list item element

  • You used the array index as a key, which breaks when items are reordered or inserted at the beginning

  • The key values are not unique, causing React to confuse items and potentially render the wrong component state

Minimal example

✗ Broken code
function ItemList({ items }) {
  return (
    <ul>
      {items.map(item => (
        <li>{item.name}</li> // no key prop!
      ))}
    </ul>
  );
}
✓ Fixed code
function ItemList({ items }) {
  return (
    <ul>
      {items.map(item => (
        <li key={item.id}>{item.name}</li> // unique key
      ))}
    </ul>
  );
}

Adding a unique key prop from the item ID lets React track each element across re-renders, preventing state loss and rendering bugs on reorders.

How to diagnose

  • Check the browser console for the React warning about missing keys in a list

  • Verify each list item has a unique and stable key prop that does not change between renders

  • Ensure keys are not array indices if the list can be reordered, filtered, or have items inserted at the start

How to fix

  • Add a unique key prop to each list item, using a stable identifier like an ID from the data

  • Generate unique IDs for items that do not have natural keys, using a library like nanoid or uuid

  • If the list is static and never reordered, using the array index as key is acceptable but not ideal

How to prevent

  • Always provide a unique, stable key when rendering lists — use the item ID or a generated unique identifier

  • Enable the React key warning in development and never ignore it — it indicates a potential rendering bug

Related Resources

Related Glossary

Related Lessons

← Back to language