How React renders · core
Reconciliation and the virtual DOM
Reconciliation is how React compares the new element tree with the current one and works out the smallest set of DOM changes, using two rules: a different type at the same position means "throw away and rebuild", and keys identify children in a list.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- React compares the new elements with its current tree (the fiber tree), not with the real DOM.
- Rule 1: same type at the same position = update the existing node and keep its state. Different type (
<div>to<span>,<A />to<B />) = unmount the whole subtree (state lost) and mount a new one. - Rule 2: keys tell React which child is which among siblings, so it can move items instead of recreating them.
- These two rules make the diff roughly O(n) instead of the O(n^3) of a general tree diff.
- State belongs to a position in the tree (plus type and key), not to the JSX tag or the variable name.
- A component defined inside another component is a new type on every render, so it remounts every time and loses its state and focus.
- The virtual DOM is not "faster than the DOM". Its value is a declarative model: you describe the UI, React batches and applies the minimal changes. Hand-written DOM code can always be as fast.
The red pen
Common traps: mistake → fix
Mistake: "React diffs against the real DOM"
Fix: it diffs the new elements against its own previous tree.
Mistake: "The virtual DOM makes React faster than vanilla JS"
Fix: it adds work; it makes UI code simpler and keeps updates reasonably small.
Mistake: "Wrapping a child in a different element keeps its state"
Fix:
<section>to<div>around a child resets the child, because the parent position changed type.
Easy to hard
Questions interviewers ask, easy to hard
- Explain reconciliation. How does React decide what to change in the DOM?
- What happens when the element type at the same position changes?
- Why is the algorithm not an O(n^3) tree diff?
- Why does a component defined inside another component lose its input focus on every keystroke?
- Is the virtual DOM faster than the real DOM? Defend your answer.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.