Question presented to candidate: "A component tree re-renders more than it should. Walk me through how you would reduce that."
What a strong answer should cover:
- Measure first. The DevTools Profiler with "record why each component rendered" tells you the cause — props, state, parent, or context — and that determines the fix.
- Know the four causes: its own state changed, its parent re-rendered, a context it reads changed, or its props changed.
- Structural fixes before memoisation: move state down (colocate it in the smallest component that needs it) and lift content up (pass the expensive subtree as
children). - Why structure first: it cannot be accidentally switched off, whereas
memois silently defeated by one inline object. - Then
React.memo— and the caveat that it needsuseMemo/useCallbackcooperation from the parent to work at all. - Context needs its own fix: split by change frequency, since
memodoes not block context propagation. - Long lists want virtualisation, not memoisation — rendering 20 rows instead of 10,000.
- The honest framing: a re-render is not automatically a problem. It is cheap unless the component is expensive or it happens very often.
- The React Compiler automates the memoisation half.
Clarifying questions expected:
- "Have we profiled, and is the re-render actually expensive or just frequent?"
- "Where does the state that triggers it live?"
Code / implementation expected: Yes — the move-state-down and lift-content-up refactors with visible counts.