When state changes, React re-runs the component function to produce a new element tree, then diffs it against the previous tree. It compares node types and keys position by position: the same type means React updates props in place, a different type means it unmounts the old subtree and mounts a new one. Only the resulting minimal set of changes is committed to the real DOM.
Why interviewers ask this
This separates people who have memorized hook syntax from people who understand the model underneath it. The interviewer wants to hear that you know rendering is not the same as committing to the DOM, that element type and key drive the diff, and that this explains real bugs like state resetting when a component moves in the tree. It also sets up follow ups about memoization and performance.
How to structure your answer
- Separate the render phase from the commit phase in your first sentence.
- Explain the diff rules: same type updates in place, different type remounts.
- Say what keys do and why they matter for lists.
- Land on a real consequence, such as state loss when a subtree remounts.
Example answer
So when state changes, React calls the component function again, and that gives it a new tree of elements. That is the render phase, and nothing has touched the DOM yet. Then it walks the new tree against the old one. At each position it looks at the element type first. If it is the same type, say a div stays a div, it keeps the existing DOM node and the existing state and just patches whatever props changed. If the type is different, it throws the whole subtree away and builds a fresh one, which means any state inside is gone. For lists it uses keys instead of raw position, so items can move without being rebuilt. Then all the collected changes get applied in one commit pass. The reason I care about this in practice is that I once debugged a form that reset itself on every keystroke, and it was because the parent rendered it inside a conditional that swapped the wrapper element type. Once you know the diff rules that is a five minute fix instead of an afternoon.
Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.
See how it worksFollow-up questions to expect
- What happens to component state when the element type changes at the same position?
- How does React batch multiple state updates inside one event handler?
- Where does the React Compiler change any of this?
Related react developer questions
Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.
Predict my questions