Lesson 4 of 80
The hook for side effects — anything that reaches outside the render itself, timed to run after the browser paints, not during render.
15 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 4
Relative to render and paint, when does a useEffect callback actually run?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
Rendering is supposed to be pure: given props and state, describe the UI, nothing else. But real components need to reach outside that — subscribe to something, fetch data, set a timer, measure the DOM. That's what useEffect is for: code that runs after React has committed and the browser has painted, deliberately kept separate from the render itself.
function Clock() {
const [now, setNow] = useState(Date.now());
useEffect(() => {
const id = setInterval(() => setNow(Date.now()), 1000);
return () => clearInterval(id); // cleanup — runs before the next effect, and on unmount
}, []); // empty array: run once, after the first paint
return <p>{new Date(now).toLocaleTimeString()}</p>;
}
The component function runs and returns JSX — a description of UI. Nothing on screen has changed yet.
The dependency array ([] here) controls when the effect re-runs: [] means once, right after the first paint; [now] would mean "re-run whenever now changes"; omitting the array entirely means "run after every single render" — almost always a mistake.
count but doesn't list it in the dependency array keeps using whatever count was when the effect was created, the same stale-closure trap from the useState lesson.useLayoutEffect exists for the rare case effect timing itself matters — it runs synchronously right after the DOM commit, before the browser paints. Good for DOM measurements that must never flash the wrong layout; bad for anything slow, since it blocks painting until it finishes."When does an effect actually run, relative to render and paint?" tests whether you understand effects as reactions to a completed render, not part of the render itself — and it's the exact question that explains why effects can safely touch the DOM or start a subscription when the render function itself can't.