Skip to the page

Hooks & effects · Concept

useEffect dependencies, cleanup, and "is it the same as an IIFE?"

The interviewer asks

Does useEffect(() => {...}, []) do the same thing as an IIFE in the component body? And how does the cleanup function work: when exactly does it run?

Then

Follow-ups the interviewer asks next

  1. Order of cleanup vs next effect when deps change.
  2. What happens with an object or array literal in deps?
  3. Do effects run during SSR?
  4. Effect timing relative to paint.

The answer

What a strong answer covers

  • An IIFE runs synchronously during every render, on the server too. An effect runs after commit (after paint for passive effects), only on the client, and with [] once per mount (twice in dev StrictMode).
  • Cleanup runs before the effect re-runs with new deps, and on unmount.
  • Dependencies are compared with Object.is, so new object or function identities re-fire the effect.
  • Effects are for syncing with external systems.

The red pen

Common traps

Avoid:"[] means it runs once ever"; side effects inside an IIFE in render.

Practise this out loud

Say your answer out loud to an AI interviewer that follows up, and get a score for it.

Practise by voice

Practise questions like this out loud

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview