Question presented to candidate: "Your component needs to fetch data, subscribe to a websocket, and start a timer. Where does that code go, and why can it not just live in the component body?"
What a strong answer should cover:
- What counts as a side effect: anything reaching outside the component's own render output — network, subscriptions, timers, manual DOM work, logging.
- Render must stay pure; effects are deliberately pushed out of it.
useEffectruns after render and after the browser paints; the dependency array controls re-runs.- The cleanup function runs before the next effect run and again on unmount — never after.
- The cancellation-flag pattern for data fetching, and why it prevents a stale response overwriting fresh state.
useLayoutEffectas the narrow synchronous exception, not a default.- Effects do not run during server-side rendering.
- Modern framing: effects are for synchronising with external systems, not for deriving state.
Clarifying questions expected:
- "Is this app server-rendered?" — it changes whether an effect can be relied on for first paint.
- "Are we on React 19? Can I use
use()and Suspense instead of a fetch effect?"
Code / implementation expected: Yes — a useEffect with a dependency array and a cleanup function. The data-fetching variant with a cancellation flag is the strongest version.