Skip to the page

Effects and synchronisation · senior

useEffectEvent: separating events from effects

useEffectEvent (React 19.2) wraps logic that is fired from an effect but should not make the effect re-run, such as reading the current theme when a chat connects; it always sees the latest values and is never a dependency.

The notes

What you must know

  • Problem: useEffect(() => { connect(roomId); conn.on("connected", () => notify("Connected", theme)); }, [roomId, theme]) reconnects the socket when only the theme changes.
  • Fix: const onConnected = useEffectEvent(() => notify("Connected", theme)); then the effect depends only on [roomId].
  • An Effect Event always reads the latest committed props and state, like a DOM event handler.
  • Rules: call it only inside effects (or other Effect Events); never during render, never pass it to children or other hooks; never put it in the deps array. The eslint-plugin-react-hooks linter enforces this.
  • Its identity changes on purpose every render, so code that wrongly depends on it breaks loudly.
  • Before 19.2 people used the "latest ref" pattern: store the callback in a ref updated after render, call ref.current() inside the effect.
  • Do not use it to hide a value that SHOULD re-sync the effect (for example the URL you log on page visit).
  • React 19.3 fixed Effect Events reading stale values inside forwardRef and memo components.

The red pen

Common traps: mistake → fix

  • Mistake: "Use useEffectEvent everywhere to silence the deps lint"

    Fix: only for logic that is truly an event fired from an effect.

  • Mistake: "Add the Effect Event to the deps array"

    Fix: it must be left out.

  • Mistake: "Call it from onClick"

    Fix: use a normal function or useCallback for event handlers.

Easy to hard

Questions interviewers ask, easy to hard

  1. Changing the theme toggle reconnects our chat socket. Fix it without suppressing the lint.
  2. Where can an Effect Event be called?
  3. Why must it not be in the deps or passed to children?
  4. How did people solve this before 19.2?
  5. When would using it hide a real dependency?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

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