Skip to the page

Effects and synchronisation · senior

You might not need an effect

Most effects in real code should be deleted: derive data during render, handle user actions in event handlers, reset state with a key, and use a framework, query library or useSyncExternalStore for fetching and subscriptions.

The notes

What you must know

  • Derived data: compute in render (const visible = todos.filter(...)), with useMemo only if expensive. Do not store it in state and sync it with an effect.
  • User events: if a click causes it (POST request, toast, analytics for the click), do it in the event handler, not in "an effect that watches state".
  • Reset all state when a prop changes: pass a key (<Profile key={userId} />).
  • Adjust part of the state on prop change: store an id and derive the selection in render; adjusting state during render is a rare last resort.
  • Chains of effects that set state and trigger more effects: compute the next state in one event handler instead.
  • Notifying a parent: call onChange in the same event handler that updates local state, or lift the state up.
  • Subscribing to an external store: useSyncExternalStore. Fetching: framework data loading, TanStack Query/RTK Query, or Server Components; if you must fetch in an effect, add a cleanup that ignores stale responses.
  • App-wide one-time initialisation: module level or a top-level guard, not an effect.

The red pen

Common traps: mistake → fix

  • Mistake: "useEffect is how you respond to state changes"

    Fix: respond to the event that changed the state.

  • Mistake: "Syncing props into state with an effect is the standard pattern"

    Fix: it causes an extra render with stale data first; compute or use a key.

  • Mistake: "Effects are free"

    Fix: each one adds a render and can show stale UI for a frame.

Easy to hard

Questions interviewers ask, easy to hard

  1. When should you NOT use useEffect? Give three misuses and the correct alternative.
  2. fullName from first and last name: effect or not?
  3. How do you reset a form when the userId prop changes?
  4. A chain of effects each sets state for the next. What is wrong and how do you fix it?
  5. Show me an effect from your codebase you would delete.

Practise

Practise with these questions

Your first interview is free

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

Start your free interview