React Keys and the Wrong-Row State Bug
How do keys affect component identity during React reconciliation? Why can using the array index as a key cause state bugs?
what they're testing: The interviewer is testing whether you can reason about identity and state preservation as a UI tree changes across renders.
Keys tell React which sibling in the new list corresponds to which sibling from the previous render. When the component type and key still match at that position, React preserves the component and its state. Change the key, and React treats that subtree as new, so its state starts over.
An array index identifies a position, not the record occupying it. Say each row keeps local input or expanded state. Insert an item at the top and every later record gets a different index. React may now keep the state associated with key={0} while passing that row a different record, so typed text or an expanded panel appears under the wrong item. Deleting, filtering, and reordering can cause the same bug.
Use a stable ID from the data, such as item.id, so the key follows the record when it moves. An index is reasonable for a genuinely fixed list whose items and order never change, though a stable ID is clearer when one exists. A changing or random key is worse because it resets the component on every render. Changing a key deliberately can be useful when a subtree should reset, such as switching between independent forms.
Why it lands
- Explains keys as identity rather than a generic performance hint.
- Walks through how state becomes attached to the wrong record.
- Gives both the normal fix and the narrow static-list exception.
- Recognizes that changing a key can deliberately reset state.
Where people slip
- "Keys only help React render faster." This misses their role in preserving component identity and state.
- "Indexes are unique, so they are safe keys." They are unique per render but not stable for a moving record.
- "Using an index always breaks a list." It does not create an identity bug when both the items and their order stay fixed.
- "A random key is safer." A changing key remounts the component and loses its state on every render.
If they push further
When is an array index safe as a key?
It avoids the usual identity bugs when the items and their order are fixed, with no insertion, deletion, filtering, or reordering. A stable data ID is still clearer when one exists.
What happens when a key changes?
React treats it as a different subtree at that position. The old subtree is removed, its state is discarded, and the new subtree starts with fresh state.
Do keys need to be globally unique, and can the component read its key?
Keys only need to be unique among siblings, and React does not pass `key` through as a prop. Pass the ID separately if the component needs it.
Sources
- React: Rendering Lists ↗react.dev
- React: Preserving and Resetting State ↗react.dev
- React: Special Props Warning ↗react.dev
Now answer it yourself.
Reading a strong answer is easy. Producing one under pressure is the skill the interview tests. Gapmap grades your answer against the same bar an interviewer would.
beta
The interviewer part is in the works.
The diagnostic, personal maps, and AI mock interviews are being finished right now. The notes stay free either way. Leave an email and you'll get the first-cohort invite, plus a month of Pro when it opens.