Overview
Curated: · Written: · Reviewed:
React Fundamentals
In one line
React calls your components to calculate the UI (render), then writes only the difference to the DOM (commit). Reach for this model whenever a bug depends on when code runs, which covers most React bugs an interviewer will raise. Rendering must stay pure, state is a snapshot per render, and effects exist only to synchronize with systems outside React.
Key definitions
- Render: calling components to compute output. Same props, state and context must give the same JSX.
- Commit: the step where React applies the computed changes to the DOM. Effects run after it.
- State snapshot: a state value is constant for one render. A setter schedules the next render and does not change the value the running code holds.
- Updater function:
setN(prev => prev + 1). Queued and applied in order during the next render. - Identity: React keeps state while the same component type stays at the same position, or the same key among siblings.
- Effect: code that keeps something external (subscription, socket, timer, widget) in sync with props and state. Cleanup runs before the next run and on unmount.
- Derived value: computed during render from props and state, never copied into state.
How it works
- An event schedules updates. React batches every update from one event (and, since React 18, from promises and timeouts) into one render.
- Render calls components. The updater queue resolves in order, and each render gets its own snapshot.
- React compares the output with the previous one and commits only the changed DOM. If nothing changed, the commit is skipped.
- Effects run after commit. Cleanup from the previous run executes before the next setup.
- Identity decides preservation. Same type at the same position, or the same key, keeps state. Anything else unmounts it.
When to use and when not to
| Situation | Use | Avoid |
|---|---|---|
| Value computable from props or state | Derive during render | Copying it into state and syncing with an effect |
| Next value depends on previous | Updater function | Repeating a captured value |
| Work caused by a click or submit | Event handler | Effect watching a flag the handler sets |
| Subscription, socket, DOM widget | useEffect with cleanup | Side effects in the component body |
| Measure, then reposition before paint | useLayoutEffect | useEffect (one frame at the wrong position) |
| Value the screen reads | State | Ref (writes do not re-render) |
| Expensive calculation, stable inputs | useMemo after profiling | memo on everything |
| Reset child per record | key={recordId} | Effect that resets state on prop change |
| Frequently changing shared value | Local state or a store with selectors | One context holding everything |
Traps interviewers probe
- Calling
setCount(count + 1)three times adds 1, because all three read the same snapshot. Fix:setCount(c => c + 1)for each call. - Defining a component inside another component's body creates a new type every render, so its state is lost on each render. Fix: move it to module scope.
- Using the array index as a key on a list that can be reordered or inserted into. Focus and state stay with the position, so typed text lands in the wrong row. Fix: key on a stable id from the data.
- Copying a prop into state. Later parent updates are ignored, and the input shows the first value. Fix: derive the value, or lift the state so the parent owns it.
- Effects without cleanup. A slower earlier response can overwrite a newer one, and StrictMode in development runs setup twice. Fix: a local
activeflag or an AbortController, returned from cleanup.
Interview tips
Open with the split: "Render is a pure calculation, commit is the only write, and effects are for outside systems." Then name the mechanism the question turns on (snapshot, identity, or cleanup) before giving the fix.
Expect these follow-ups:
- Why does StrictMode run component bodies twice, and what does that expose? Impure renders and missing cleanup.
- When is an updater function needed? When the next value depends on the previous one.
- Why does a key change reset state? Identity changes, so React unmounts and mounts a new subtree.
- Why does a context change re-render everything? Consumers compare the provider value by identity, so an object literal changes on every render.
- Where should use client go? On the smallest interactive leaf, so server-only imports stay out of the bundle.
30-second outline: "State is a snapshot for each render, so updates queue and a handler cannot read its own new value. Render must stay pure, and commit is where DOM changes happen. Keys set identity, and a changed key or type resets state. Effects synchronize with external systems and need cleanup, which StrictMode checks in development. I would derive values instead of storing them and profile before memoising."
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.