Keys, Reconciliation, and Remounting: The Real Rules
Why keys matter beyond lists: how reconciliation decides to preserve state or remount, why index keys break UIs, and how to debug key-related bugs.
On this page
What you’ll learn
- What keys actually do in React (and what they don’t)
- When React preserves state vs remounts
- Why index as key causes subtle production bugs
- Practical rules for stable keys
The job of a key
A key answers one question:
“Is this child the same logical item as before?”
Keys help React match old children to new children during reconciliation. Use a stable, unique key such as an id from your data.
- Stable key → React preserves component instance + state
- Unstable key → React remounts, losing state (and re-running effects)
The classic bug: index as key
{items.map((item, index) => (
<Row key={index} item={item} />
))}If the list changes order (insert at top, sort, filter), React may reuse a Row for the wrong item.
Symptoms:
- Input values jump rows
- “Expanded” state sticks to the wrong item
- Animations glitch
Fix
Use a stable ID from the data:
{items.map((item) => (
<Row key={item.id} item={item} />
))}When is index as a key acceptable?
Only when all of these are true:
- the list is static and never changes order
- items are never inserted or removed
- the list is never filtered
In an interview, the safe answer is: "Index keys are risky, so I use stable ids. I'd only use the index for a static list."
What about random keys?
Also wrong:
<Row key={Math.random()} item={item} /> // ❌The key changes on every render, so React remounts every row every time, which resets their state and throws away the DOM.
Remounting: how to force it (and when it’s useful)
Sometimes you want a full reset.
Example: reset a form when the user switches accounts.
<Form key={userId} />Changing the key tells React: “This is a new component. Remount it.”
Interview questions
Q1) Why are keys required?
30-second answer: They let React match items between renders so it preserves state correctly and updates efficiently.
Follow-up: Why is index key dangerous?
Q2) What does changing a key do?
30-second answer: It forces React to treat the component as new and remount it (state reset + effects rerun).
Q3) When is index as a key fine?
30-second answer: Only for a static list that is never reordered, filtered, or changed. Otherwise identity becomes position-based and state attaches to the wrong item.
Quick recap
- Keys define identity during reconciliation
- Index keys break when order changes; they're only safe for static lists
- Random keys remount everything on every render
- Changing key intentionally remounts
Looking for structured, offline interview prep?
Free online lessons • Portable Amazon Kindle / Paperback
Frontend Interview Guide (2025 Edition)
By Rajesh Dhiman
Comprehensive preparation covering React internals, Web Performance, CSS architecture, and system design questions.
Frontend Interview Guide (2025 Edition)
By Rajesh Dhiman
Comprehensive preparation covering React internals, Web Performance, CSS architecture, and system design questions.
Note: All lessons on Frontend Interview remain completely free to read online. As an Amazon Associate I earn from qualifying purchases.
Written by Rajesh Dhiman
Authored by Rajesh Dhiman and engineered at Eunix Tech. Focused on deep-dive frontend engineering, JavaScript runtime internals, React architecture, and web performance.