Skip to content
tezvyn:

How does React use keys in reconciliation? When do keys cause bugs?

Source: react.devHardHow cards are made

How does React use keys in reconciliation? When do keys cause bugs?

Tests reconciliation identity semantics. Answer: keys give scoped identity for O(n) diffing; stable keys preserve state, but indices during reordering make React reuse DOM nodes wrongly, polluting state. Red flag: saying keys are 'just for performance'.

What's really being asked

Whether you understand that React keys are not decorative attributes but identity primitives used by the reconciliation diffing algorithm. The interviewer wants to know if you grasp sibling-scoped identity, state preservation versus DOM node reuse, and the difference between a remount and a re-render.

The full answer

First, that keys are only compared among siblings in the same array and tell React which element corresponds to which across renders. Second, that stable keys allow React to preserve component state and underlying DOM nodes when items move, which is the O(n) diffing optimization. Third, that unstable keys such as array indices or random values destroy this mapping, causing React to treat moved items as deletions and insertions or to incorrectly reuse DOM nodes. Fourth, the distinction between performance degradation from unnecessary remounts and state-related bugs from incorrect node reuse.

The mistakes people make

Calling keys a performance optimization without mentioning identity or state preservation. Saying keys must be globally unique rather than unique only among siblings. Recommending Math.random as a key source, which guarantees remounting on every render. Claiming array indices are always safe, or conversely claiming they are always wrong, without noting that indices are only problematic when the list order changes or items are inserted and deleted. Describing the virtual DOM diff as operating on the entire tree rather than sibling arrays.

What usually comes next

How would you key a list where items do not have natural unique IDs? What happens to refs and useEffect cleanup when a component is remounted because its key changed? How does React handle keys when fragments are involved? Can two sibling components share the same key if they are in different arrays?

A concrete example

Imagine a todo list rendered with map where each row contains a text input and a delete button. If you use the array index as the key and delete the first todo, React sees that index zero now holds different data. Because the key stayed zero at that position, React reuses the existing DOM node and associated component state for the old first item. The second todo inherits the first input's focus, cursor position, and any uncontrolled value, while the deleted todo's state appears to persist in the wrong row. If the rows instead used stable database IDs as keys, React would correctly unmount the deleted row and shift the remaining ones without state pollution.

Interview question

Why can using array indices as keys cause state pollution when a list is reordered?

  • a.React uses keys purely to optimize diffing speed, so indices only slow down reconciliation without affecting which component state is attached to which DOM node.
  • b.React requires globally unique keys, so indices fail when multiple lists render and cause cross-component state leakage during reconciliation.
  • c.React interprets changed indices as new components, forcing a full remount of the list and resetting all state and focus on every update.
  • d.React relies on keys to identify elements across renders; indices make React match DOM nodes to the wrong data, preserving state for the wrong item.Correct
Why?

Keys provide sibling-scoped identity, not just a performance shortcut; when indices are reused after reordering, React incorrectly associates the old DOM node and its state with new data. Option A is wrong because keys fundamentally determine state preservation, so unstable keys create correctness bugs rather than merely slower reconciliation.

Just read this? Test yourself on what you have been reading.

Read the original → react.dev

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on react — each one lists the topics its interview covers.

See open roles