Skip to content
ClapelloNotes by Barath
ReactCh 15 · 6/6
Deep dive4 min readUpdated

React / 87

useEffectEvent: read the latest without re-running

TL;DR

Some code inside an effect should see the latest props but shouldn't make the effect re-run. Move it into an Effect Event with useEffectEvent, added in React 19.2.

Reactive vs non-reactive

reactive · in the dependency array

useEffect

connect(orderId)

orderId changes → disconnect and reconnect. That's the point of the effect.

non-reactive · not a dependency

useEffectEvent

showToast(status, { sound: soundOn })

soundOn changes → nothing re-runs. The next toast just reads the new value.

Values the effect syncs with stay as dependencies. Values only read when something happens move into the Effect Event, which always sees their latest version.

Ask of every value inside the effect: should a change to this re-run the effect?

  • orderId: yes. A new order needs a new connection. It's reactive and stays in the array.
  • soundOn: no. It only matters at the moment a toast is shown. It's non-reactive, so the code that reads it moves into an Effect Event.

Before and after

OrderTracker.jsx
import { useEffect } from "react";
import { connectToOrderFeed } from "./orderFeed";
import { showToast } from "./toast";
 
export default function OrderTracker({ orderId, soundOn }) {
  useEffect(() => {
    const feed = connectToOrderFeed(orderId);
    feed.on("status", (status) => {
      showToast(`Order ${status}`, { sound: soundOn });
    });
    return () => feed.close();
  }, [orderId, soundOn]); // soundOn changes → reconnect
 
  return <p>Tracking order {orderId}</p>;
}
OrderTracker.jsx
import { useEffect, useEffectEvent } from "react";
import { connectToOrderFeed } from "./orderFeed";
import { showToast } from "./toast";
 
export default function OrderTracker({ orderId, soundOn }) {
  const onStatus = useEffectEvent((status) => {
    showToast(`Order ${status}`, { sound: soundOn }); // always the latest soundOn
  });
 
  useEffect(() => {
    const feed = connectToOrderFeed(orderId);
    feed.on("status", (status) => onStatus(status));
    return () => feed.close();
  }, [orderId]); // onStatus is not a dependency
 
  return <p>Tracking order {orderId}</p>;
}

connectToOrderFeed and showToast stand in for your own feed and toast code. Now changing orderId reconnects, and changing soundOn doesn't, yet the next toast still uses the new setting.

Reconnect, or just read the latest?

Try it
[orderId, soundOn]connects: 1

Plain effect: the toast reads soundOn, so it's a dependency.

  1. connect #101
[orderId] + useEffectEventconnects: 1

The toast lives in an Effect Event. Only orderId is a dependency.

  1. connect #101
A simulation of the two versions in this lesson. Toggle the sound: the plain effect reconnects, the Effect Event version doesn't. Then send a status: both toasts use the new setting.

The rules

RuleWhy
Declare it at the top level of the same component or hook as its effectIt's a hook, and the linter links it to "its" effect
Call it only from inside effects (or other Effect Events)It's meant for things that happen while the effect is running
Never call it during render or from a click handlerHandlers already see the latest values. Use a normal function
Never pass it to other components or hooksIt belongs to the effects of its own component. Pass a normal function or a useCallback instead
Never put it in a dependency arraySame reason: it would re-run the effect on every render

useEffectEvent arrived in React 19.2. Upgrade eslint-plugin-react-hooks too: its latest versions know these rules and stop asking you to list Effect Events as dependencies.

Gotchas

Recall

Can you answer these?

0/5

Answer out loud (or in your head) before you tap. That's what makes it stick.

Saved on this device only. No login.