Clean technical diagram of React useEffectEvent filtering reactive props out of an Effect's dependency array

React Effects often need a callback: a subscription handler, a timer tick, a WebSocket onmessage. The moment that callback reads a prop or state, you hit a fork in the road. Add it to the dependency array and the Effect re-runs — reconnecting, resubscribing, resetting timers — every time the value changes. Leave it out and you capture a stale value forever. React 19.2 adds a purpose-built third option: useEffectEvent.

The problem: reactive values inside non-reactive callbacks

Suppose a chat connection should reconnect only when roomId changes, but its connected handler needs to read the current theme to show a notification:

import { useEffect } from 'react';

function ChatRoom({ roomId, theme }) {
  useEffect(() => {
    const connection = createConnection(roomId);
    connection.on('connected', () => {
      notify(`Connected to ${roomId}`, theme);
    });
    connection.connect();
    return () => connection.disconnect();
  }, [roomId, theme]); // toggling theme drops and rebuilds the socket

  return <h1>Room: {roomId}</h1>;
}

Including theme means every theme toggle tears down the socket. Omitting it means the handler keeps the theme from the render that created the connection. Both are wrong.

The old workarounds (and why they grate)

  • Mirror the value in a ref, update the ref in an Effect, then read ref.current in the callback. It works, but one piece of logic is spread across three places.
  • Wrap the callback in useCallback and add it to the deps. Its identity still changes when the value changes, so you are back to resubscribing.
  • Add // eslint-disable-next-line react-hooks/exhaustive-deps. Fastest today, most likely to become a stale-closure bug six months from now.

The fix: useEffectEvent

useEffectEvent declares a function that lives in the Effect’s world but is deliberately not part of the reactive dependency graph. Each call reads the latest props and state from the most recent committed render — but changing those values never re-runs the Effect that calls it.

import { useEffect, useEffectEvent } from 'react';

function ChatRoom({ roomId, theme }) {
  // Non-reactive: always sees the latest theme and roomId.
  const onConnected = useEffectEvent(() => {
    notify(`Connected to ${roomId}`, theme);
  });

  useEffect(() => {
    const connection = createConnection(roomId);
    connection.on('connected', () => {
      onConnected();
    });
    connection.connect();
    return () => connection.disconnect();
  }, [roomId]); // theme is intentionally NOT a dependency

  return <h1>Room: {roomId}</h1>;
}

Switching roomId reconnects — correct. Switching theme only changes what the next onConnected() call sees — also correct, with zero reconnects. The callback can also read roomId freshly without adding it twice; the Effect dependency still governs the actual connection.

A second case: callbacks that must not re-subscribe

import { useEffect, useEffectEvent } from 'react';

function DataFeed({ url, onData }) {
  const handleData = useEffectEvent((data) => {
    onData(data); // latest parent callback, no stream restart
  });

  useEffect(() => {
    const source = new EventSource(url);
    source.onmessage = (event) => handleData(JSON.parse(event.data));
    return () => source.close();
  }, [url]);

  return null;
}

The parent can pass a new onData on every render without reopening the stream. The value that defines what is subscribed to (url) stays reactive; the handler does not.

Rules and gotchas

  • Version gate: requires React 19.2+. On older versions the named import is undefined; check your installed React before shipping.
  • Call site: only call an Effect Event from inside an Effect (or from another Effect Event). It is not a general-purpose onClick handler and must not be called during render.
  • Hook rules: define it at the top level of your component, never inside a loop or condition.
  • The Effect is still reactive: anything that changes what the Effect subscribes to (roomId, url) belongs in the dependency array. Move only callback-style logic into the Effect Event.
  • Linter: eslint-plugin-react-hooks v6+ understands Effect Events and will not ask you to list them as dependencies — it will actually flag doing so. Upgrade the plugin instead of suppressing the rule.
  • No forced sync: Effect Events read values from the latest committed render. They are not a trick for reading state synchronously mid-render.
  • Don’t hide real dependencies: if changing a value should genuinely tear down and restart the work, keep it reactive. Wrapping it in an Effect Event is how stale-connection bugs return.

When to reach for it

  • Subscription callbacks that read props/state: socket.onmessage, addEventListener, EventSource, third-party SDK listeners.
  • Timers and intervals whose callback needs fresh values but whose schedule must not reset.\- Observers (ResizeObserver, IntersectionObserver) whose handler reads changing configuration.

Skip it when the callback itself is the thing that should restart the Effect — a new url, a new roomId. Keep those values reactive.

TL;DR

  • Reactive value inside an Effect callback? Adding it to deps causes unwanted re-runs; omitting it causes stale closures.
  • useEffectEvent gives you a non-reactive callback: fresh values on every call, no Effect re-subscription.
  • Keep the subject of the subscription in the deps; move only the handler into an Effect Event.
  • React 19.2+, ESLint plugin v6+, call from Effects only.