Lesson 6 of 80
How React decides which real DOM changes are actually needed — and why a list without stable keys can attach state to the wrong item.
14 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 4
A list uses key={index}. A new item is inserted at the front, shifting every other item's index by one. What's the likely consequence?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
Every render produces a brand-new description of the UI. React doesn't throw away the real DOM and rebuild it from scratch each time — that would be slow and would lose things like input focus and scroll position. Instead, reconciliation compares the new description to the previous one and computes the smallest set of real DOM changes needed to catch up.
For a list of children, React needs a way to tell "this is the same item, just moved" apart from "this is a different item entirely." That's what key is for.
{items.map((item, index) => (
<Row key={index} item={item} /> // ❌ index as key
))}
If items gets reordered — say a new item is inserted at the front — every item's index shifts, even though the items themselves didn't change. React matches by key, so it now thinks "index 0" is still the same Row it was before, just with different props. It reuses that DOM node and whatever internal state it was holding — attaching the wrong item's state to the new item now sitting at that position.
Three items render as a list. Item B has its own local state — an input holding “hello”.
<Row> swapped for a <div>), losing any transition/animation state on it too."Why shouldn't you use the array index as a key?" is one of the most common React interview questions precisely because the failure mode is subtle — nothing crashes, no warning is guaranteed to be obvious, state just silently ends up attached to the wrong row. Explaining the actual mechanism (React matches by key, not by data) is what separates "I know the rule" from "I know why the rule exists."