Explain reconciliation through the remount bug: a changed element type, an unstable key, or a shifted position makes React discard a subtree and its state. Covers the Virtual DOM, the O(n) diffing heuristics, render vs commit, and the key mistakes behind form resets and wrong-row state.
Frontend interview practice question
Why did React remount my component instead of updating it? Reconciliation, keys, and element type
Interview quick answer
Interview focus
This React interview question tests whether you can explain React Remounts Instead of Updating: Keys and Type, connect it to production trade-offs, and handle common follow-up questions.
- React Remounts Instead of Updating: Keys and Type explanation without falling back to memorized definitions
- Rendering and Diffing reasoning, edge cases, and production failure modes
- How you would answer the most likely React interview follow-up
Use this React interview question to rehearse a quick answer, common mistake, follow-up, and production pitfall.
Full interview answer
The bug to explain
A form resets while the user is typing, or a list row keeps the wrong state after a reorder. React did not update the component; it remounted it. Reconciliation decides, per position in the tree, whether the next element is the same identity as the previous one: same type and same key means update in place and keep state; a different type, a different key, or a shifted position without keys means unmount and mount fresh. The diffing heuristics below are the rules behind that decision, and bad keys are the most common way to trigger it by accident.
// Same position, different element type: React remounts the subtree
function Panel({ compact }) {
return compact ? <section><Editor /></section> : <div><Editor /></div>;
}
// Toggling compact throws away Editor's local state.
// Index keys pin state to the slot, not the item
{items.map((item, index) => <Row key={index} item={item} />)} // row state sticks to the slot after a reorder
{items.map((item) => <Row key={item.id} item={item} />)} // row state follows the item
Overview
React’s diffing algorithm is the under-the-hood heuristic that makes reconciliation fast enough to use constantly. Instead of computing the perfect tree edit distance, React makes a few bets about element type and keys. Those bets are why list reordering with bad keys creates state bugs, and why the algorithm can feel surprising if you expect a full semantic understanding of your UI.
Rule | Explanation |
|---|---|
1. Element Type Comparison | If two elements have the same type (e.g., both are |
2. Replacement on Type Change | If the element type differs (e.g., |
3. Key-Based List Comparison | When diffing lists, React uses keys to track which items changed, were added, or removed. Keys ensure minimal reordering and preserve component state. |
// Example: Efficient list diffing using keys
const fruits = ['Apple', 'Banana', 'Cherry'];
// When re-rendered with a new list:
const updatedFruits = ['Banana', 'Cherry', 'Apple'];
// React uses 'key' to determine what moved instead of re-rendering everything.
{updatedFruits.map(fruit => (
<li key={fruit}>{fruit}</li>
))}
How the Algorithm Works
- Step 1: React starts from the root of the tree and compares each Virtual DOM node with its previous version.
- Step 2: If nodes are identical, React skips them.
- Step 3: If attributes differ (e.g.,
className), React updates only those attributes in the real DOM. - Step 4: When lists change, React uses keys to identify additions, deletions, and reorderings efficiently.
Example of Attribute Update
React updates only what changes — not the entire node.
// Old Virtual DOM
<div className='red'>Hello</div>
// New Virtual DOM
<div className='blue'>Hello</div>
// React result:
// Only updates the className from 'red' to 'blue', not the entire <div>.
Performance Advantage
The traditional approach to comparing trees is O(n³), but React’s diffing algorithm achieves near O(n) by making these assumptions:
- Different element types produce different trees.
- Stable keys indicate the same components across renders.
When Keys Are Misused
Incorrect or missing keys can cause React to discard and re-create DOM nodes unnecessarily, resulting in performance issues and lost component state. Always use unique and stable keys (like database IDs).
Preserve vs replace: the reconciliation decision
Reconciliation is React’s decision process for component identity: should this subtree be preserved, updated in place, or remounted? Changing an element type or using an unstable key can reset local state even when the UI looks almost the same. Diffing is one implementation detail inside that bigger preserve-vs-replace decision.
Phase | What React does | Rules / why it matters |
|---|---|---|
Render phase | Runs components to compute the next UI tree | Should be pure (no subscriptions, network calls, DOM writes). React may re-run renders (especially in dev/StrictMode). |
Reconciliation | Matches old vs new elements (type + key + position) and decides what to keep, move, mount, unmount | Keys are crucial for stable identity in lists; changing element type at a position can cause remount/state loss. |
Commit phase | Mutates the real DOM + runs effects/lifecycles | This is where DOM changes actually happen; effects run after commit. |
Virtual DOM: the input to reconciliation
The Virtual DOM is React’s in-memory description of what the UI should look like after a render. React can re-run components, compare trees, and still commit zero DOM changes. That distinction explains why some “performance issues” are really JS/render work or key/identity mistakes, not raw DOM mutation cost.
Claim | Accurate? | Better wording / why it matters |
|---|---|---|
“VDOM is a copy of the real DOM.” | ⚠️ half-true | It’s a JS representation of the UI ( |
“React updates the minimal set of DOM changes.” | ⚠️ usually | React uses reconciliation heuristics. With stable keys/types, it’s very efficient, but it’s not a perfect minimal-edit solver. |
“Re-render means the DOM changed.” | ❌ | Re-render means React re-ran components to compute a new UI tree. DOM mutations only happen in the commit phase if something actually differs. |
“VDOM makes React fast.” | ⚠️ incomplete | The win is: predictable re-rendering + efficient diffing + batching. Many perf issues are still JS work, reconciliation work, or browser layout/paint. |
Rules of reconciliation
- If an element type changes (e.g.,
<div>→<span>), React destroys and rebuilds that node. - Keys give elements identity across renders, especially in lists, so React updates or moves items instead of re-creating subtrees.
- Keys must be unique and stable (like database ids); index keys are only safe for static lists.
<li>Elements of the same type are compared recursively.The diffing algorithm is React’s performance superpower — it ensures your UI updates surgically instead of rebuilding the whole DOM every time something changes.
Summary
- React’s diffing algorithm compares Virtual DOM trees efficiently.
- It updates only changed attributes or elements using heuristic assumptions.
- Keys play a critical role in optimizing list updates and preserving state.
Use this as one explanation rep, then continue with the React interview questions cluster or a guided prep path.
Built in public
The code behind FrontendAtlas lives on GitHub. A star helps other frontend engineers find it.