React / 87
useEffectEvent: read the latest without re-running
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.
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
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>;
}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: 1Plain effect: the toast reads soundOn, so it's a dependency.
- connect #101
[orderId] + useEffectEventconnects: 1The toast lives in an Effect Event. Only orderId is a dependency.
- connect #101
The rules
| Rule | Why |
|---|---|
| Declare it at the top level of the same component or hook as its effect | It'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 handler | Handlers already see the latest values. Use a normal function |
| Never pass it to other components or hooks | It belongs to the effects of its own component. Pass a normal function or a useCallback instead |
| Never put it in a dependency array | Same 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.
