React Rendering Deep Dive and a Re-render Fix Walkthrough
BehavioralModel answerLast updated
Explain how React decides to render, reconcile and commit updates, then present a measured example of finding and fixing a rendering performance or correctness bug in a real app.
You are in a frontend technical screen. The interviewer asks you to pick a React application you have worked on, use it to explain how React renders, and then describe a rendering problem you diagnosed and fixed in it.
Assume a client-rendered app on React 18 or later unless you say otherwise. State that assumption at the start, and mention server rendering and hydration only briefly if relevant.
Part 1: How React renders
Explain:
- What causes a component to render, including triggers that surprise people.
- What happens in the render phase and in the commit phase, and when effects run.
- How React compares the previous and next element trees to decide which DOM changes to make, including the roles of element type and keys.
- How React batches state updates.
Part 2: A rendering problem from your experience
Describe a concrete case where rendering caused a performance or correctness problem. Cover:
- The symptom and how you measured it.
- The root cause, explained using the mechanics from Part 1.
- The fix, its trade-offs, and a before-and-after measurement.
- What you learned or would watch for next time.
Aim for a spoken answer of roughly five to eight minutes. Be specific about your own role.
Hints
Hint 1
Separate three ideas clearly: a component function being called (render), React computing a diff, and the browser DOM actually being mutated (commit). A re-render does not imply a DOM update.
Hint 2
For Part 2, pick one story with a measurable symptom (for example slow typing in an input, a janky list, or a wasted-render count in the Profiler) and structure it as situation, diagnosis, fix, result.
Hint 3
Justify each fix with a measurement and name its cost. Memoization has overhead and can be defeated by unstable props, so explain why your fix addressed the actual cause rather than applying it everywhere.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Framing
I would open by stating my assumptions: a client-rendered app on React 18+, using function components and hooks. Then I would split the answer into mechanics and a real story, and keep the story tied to the mechanics.
Part 1: How React renders
What triggers a render
A component renders when:
- Its own state changes (
useStatesetter oruseReducerdispatch with a value that is notObject.is-equal to the current one). - Its parent renders, which by default re-renders all children, even if their props are unchanged. This is the most common surprise.
- A context it consumes changes value. Every consumer re-renders, even if it uses only a slice of the value.
- An external store it subscribes to changes (for example via
useSyncExternalStore).
Props changing is not itself a trigger. Props change because a parent rendered. React.memo can skip a child render when the parent renders but the props are shallow-equal.
Render phase versus commit phase
- Render phase: React calls your component functions and produces a tree of elements. This must be pure: no mutations, no subscriptions, no network calls. In concurrent rendering, React may pause, restart or discard this work, and in development Strict Mode intentionally double-invokes it to expose impurities.
- Commit phase: React applies the minimal set of DOM mutations, updates refs, then runs effects.
useLayoutEffectruns synchronously after DOM mutation and before paint.useEffectnormally runs after paint.
The key point: a component can render many times without the DOM changing at all. Wasted renders cost CPU, not DOM writes.
Reconciliation
React compares the new element tree with the previous one, using two heuristics that keep the diff roughly linear:
- Different element type at the same position: the old subtree is unmounted (state lost) and the new one mounted. The same type keeps the instance and its state, and React updates props.
- Lists use keys to match children across renders. Stable, unique keys let React preserve state and move DOM nodes instead of recreating them.
Common key mistakes:
- Using the array index as a key in a list that can reorder, insert or delete. State such as input text then attaches to the wrong row.
- Generating keys with
Math.random()or at render time, which forces remounting every render. - Defining a component inside another component, which creates a new type each render and remounts the subtree.
Keys are also a deliberate tool: changing a key resets a component's state, which is often cleaner than syncing state with an effect.
Batching
Since React 18, updates are batched automatically, including inside promises, timeouts and native event handlers. Several setState calls in one tick produce one render. Updates based on previous state should use the functional form (setCount(c => c + 1)), because the state value captured in a closure is a snapshot. Transitions (startTransition) mark updates as non-urgent so urgent input stays responsive.
Part 2: A model story
Replace the details with your own real experience. The structure is what matters.
Situation. A dashboard had a filterable table of around 2,000 rows. Typing in the search box lagged noticeably, with input latency of roughly 250 ms per keystroke on a mid-range laptop.
Diagnosis. I used the React DevTools Profiler to record a keystroke. The flame graph showed the whole page rendering on each keystroke, with the table rows dominating the commit time. Two causes appeared:
- The search text lived in state at the page root, so every keystroke re-rendered the entire tree (trigger 2: parent render).
- Rows were memoized, but the memoization was defeated because the parent passed a new inline callback and a freshly created options object on each render, so shallow comparison failed.
I also confirmed with the browser Performance panel that scripting time, not layout or paint, was the bottleneck. That told me DOM updates were not the problem, rendering work was.
Fix.
- Moved the input state down into a small search component and kept the committed filter value in the parent, updated through
startTransitionso typing stayed urgent while filtering was deferred. - Wrapped the row component in
React.memoand stabilized its props withuseCallbackanduseMemo, so unchanged rows skipped rendering. - Computed the filtered list with
useMemokeyed on the data and the filter value. - Fixed keys to use the record ID rather than the index, which also fixed a bug where inline-edit state jumped between rows after sorting.
- For the very large case, I considered list virtualization and noted it as the next step rather than adding it prematurely.
Result. Rows rendered per keystroke dropped from about 2,000 to the few that changed, and input latency fell from about 250 ms to under 50 ms. I verified with the same Profiler recording before and after, and I added a note in the PR with the numbers.
Trade-offs. Memoization adds comparison cost and code complexity, and it breaks silently when someone later adds an unstable prop. Moving state down is the cheapest fix and should be tried first. The transition makes the table update slightly after typing, which was acceptable here.
Lessons.
- Measure first. Blanket
memo,useMemoanduseCallbackoften add cost without benefit. - Prefer structural fixes (colocate state, split components, pass children as props) before memoization.
- Treat keys and effect dependencies as correctness concerns, not only performance concerns.
Common mistakes to avoid
- Saying "React re-renders the whole DOM" or confusing a re-render with a DOM update.
- Claiming
useMemoguarantees caching. It is a performance hint that React may discard. - Putting derived data into state and syncing it with
useEffect, which causes extra renders and stale values. - Describing an optimization with no measurement, or a story where your own contribution is unclear.
- Forgetting that context value changes re-render all consumers, and not memoizing the provider value.
Follow-ups to be ready for
- How would the answer change with server rendering and hydration?
- When would you reach for virtualization, or a state library with selector-based subscriptions?
- How does the React Compiler change the need for manual memoization?