Find: Index as Key
Find the Mistake Medium
Question
What is wrong with using index as the key here?
Code
{items.map((item, index) => (
<li key={index}>{item.name}</li>
))}
// items can be reordered or items can be removed
Hint
What happens to indices when items are removed or reordered?
Explanation
Using the array index as key is problematic when items can be reordered, inserted, or removed. Indices shift, causing React to incorrectly reuse DOM nodes and leading to subtle state bugs. The fix is to use a stable unique ID: key={item.id}.
Related Resources
Related Lessons
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.