Question presented to candidate:
"What is useEffect for — and, just as importantly, what is it not for?"
What a strong answer should cover:
- The modern framing: an effect synchronises a component with an external system — the network, a subscription, the DOM, a timer. It is not a general-purpose lifecycle hook.
- It runs after render and after the browser paints, so it never blocks the visible update.
- The dependency array decides when it re-runs; the returned cleanup undoes the previous run.
- The most valuable half of the answer: when not to use one. Derived values belong in render; user-action responses belong in event handlers.
- Deriving state in an effect costs an extra render pass and leaves a moment where the UI shows the stale value.
- Effects are an escape hatch from the React paradigm — the React docs categorise them that way deliberately.
- Common wrong uses: transforming data for display, resetting state on prop change (use
key), and doing work that belongs in a submit handler. useLayoutEffectas the pre-paint exception;useEffectEventfor non-reactive logic inside an effect.
Clarifying questions expected:
- "Is this synchronising with something outside React, or transforming data we already have?" — that single question decides whether an effect belongs at all.
Code / implementation expected: Yes — an effect that genuinely synchronises, beside a derived value that should not be one.