Q&A-2How would you reason about components as pure functions of props and state in a system you own?
Answer
The contract. A component's render must be a pure function of its inputs: props, state, context, and any store snapshot it reads. Same inputs, same output, no observable side effects. That is not a style preference: React's render phase is allowed to run any number of times, be interrupted, restarted, or thrown away. StrictMode double-invokes every component in development to surface violations. Concurrent rendering can start a render, abandon it, and start again. React Compiler memoizes on the assumption that render is pure. If your render writes to anything outside itself, the output depends on how many times React happened to call it, and that count is not part of your contract.
Where the boundary sits. Render phase: compute. Commit phase: effects, refs, event handlers: touch the world. Anything that must vary between renders (a timestamp, a random id, a subscription) comes in as an input or is read in an effect, never generated during render.
Trace: the double-invoke.
let renders = 0; // module scope
function Counter({ step }) {
renders += 1; // write during render
const [n, setN] = useState(0);
return <button onClick={() => setN(n + step)}>{n} (render #{renders})</button>;
}Mount under <StrictMode> in dev: React invokes Counter twice and keeps the second result. renders goes 1 → 2, so the committed DOM reads 0 (render #2). In production, a single invoke gives 0 (render #1). Same props, same state, different output: the definition of impure. A test that renders once and asserts on the string passes in CI and fails the moment StrictMode is on.
Trace: the bailout. Purity also governs how you update state.
const [items, setItems] = useState([{ id: 1, done: false }]);
function toggle(id) {
const next = items; // same array reference
next.find(i => i.id === id).done = true; // mutate in place
setItems(next); // Object.is(next, items) === true
}React compares the new state to the old with Object.is. Same reference → it bails out, no re-render, the checkbox stays stale. The fix is to produce a new object: setItems(items.map(i => i.id === id ? { ...i, done: !i.done } : i)).
Where each kind of value belongs.
| Value | Home | Why |
|---|---|---|
| Derived from props/state | computed in render (useMemo only if measured hot) | always consistent with inputs |
| Mutable, not part of output (timer id, DOM node, previous value) | useRef | survives renders, doesn't trigger one |
| External store | useSyncExternalStore | React reads a snapshot, no tearing |
| Sync with the outside world (fetch, subscription, DOM) | useEffect | runs after commit; may run twice in dev |
Trade-offs. Purity costs recomputation: every render redoes the work. useMemo, useCallback and React.memo are caches layered on a pure function: they change cost, not semantics, and they only work if the function is pure. Reach for them when a profile shows the recompute is the bottleneck, not by default. The escape hatch for genuinely imperative work (canvas, focus management, a charting library) is a ref plus an effect, not a render-phase write.
In a system you own, make the contract enforceable rather than aspirational: StrictMode on in dev, eslint-plugin-react-hooks and the React Compiler lint rules failing the build, and at least one test that renders a component twice and asserts identical output. That last one catches the class of bug that otherwise only appears under a fast refresh or a concurrent interruption.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.