Course contentsShow
← React
Lesson 162 of 2,3965. Lists and KeysFree lesson

The Index-as-Key Pattern and Why It's Common

Understanding why developers reach for index as key and the scenarios where it superficially appears to work fine.

The Index-as-Key Pattern and Why It's Common

What you'll learn: Why developers frequently use array index as the key prop, and when it seems harmless.

Why It's Everywhere

You've seen it countless times in tutorials and real codebases:

{items.map((item, index) => (
  <li key={index}>{item.name}</li>
))}

Using the array index as a key is tempting because it's always available. You don't need database IDs, you don't need to install a library to generate unique identifiers — the .map() function hands you an index for free.

When It Appears to Work Fine

For many simple cases, index-as-key causes no visible problems:

  • Static lists that never change (a hardcoded array of links)
  • Append-only lists where you only add to the end
  • Lists without user interaction (no reordering, no deletions)
  • Lists where components have no internal state

In these scenarios, the list items maintain their position, and React's reconciliation doesn't produce unexpected behavior. Everything renders correctly, no bugs appear, and developers move on.

The Slippery Slope

Because it works in simple cases, many developers adopt it as their default pattern without understanding the consequences. It becomes muscle memory. The problems only surface later when requirements change — suddenly the list needs sorting, or items get deleted, or each item has a checkbox that should preserve its checked state.

Key Takeaway: Index-as-key is common because it's convenient and often appears to work, but it's only truly safe for static, unchanging lists where item identity doesn't matter. Understanding why it works in simple cases helps you recognize when it will fail in complex ones.