Why You Should Avoid Using Array Index as a key in React, The real reason...
When working with lists in React, you’ve probably seen this warning:
“Each child in a list should have a unique key prop.”
Many developers silence it by using the array index as the key. It works… until it doesn’t. Let’s talk about why this is a bad idea and what’s happening under the hood.
How React Uses Keys (Fiber & Reconciliation)
React uses a Virtual DOM and a process called reconciliation (powered by Fiber) to efficiently update the UI.
During reconciliation, React:
Keys are critical because they help React:
Keys must be stable and unique.
What Goes Wrong with Array Index as Key
Imagine this scenario:
From React’s perspective:
As a result:
Recommended by LinkedIn
Mutations Make It Even Worse
Using array index as a key completely breaks down when:
All subsequent indices shift, but React still thinks:
“Same key = same component”
This confuses the diffing algorithm and leads to incorrect UI updates.
The Right Approach
Always use a stable, unique identifier from the data itself:
items.map(item => (
<Component key={item.id} {...item} />
))
Why this works:
Using the array index as a key might seem harmless, but it silently breaks React’s reconciliation logic — especially in dynamic lists.
So next time you’re tempted to write key={index}… pause, and give React a key it can trust