useEffectEvent and Stable Effect Logic
Build, explain and debug React 19 interfaces: components, rendering, state, effects, identity, forms and actions, server components, concurrency, performance and testing, ending with architecture and typing work on real component challenges.
What you will be able to do
#- Separate reactive values from non-reactive logic inside an effect.
- Read the latest props and state without adding an effect dependency.
- Recognize where useEffectEvent is not allowed.
Overview
#Non-reactive logic inside an effect
Some effects mix reactive values, which should re-run the effect when they change, with non-reactive values, which should be read fresh but not trigger a re-run. A chat connection that reconnects when the room changes but should read the latest theme without reconnecting is the classic example. Putting the theme in the dependency array reconnects on every theme change; omitting it captures a stale theme.
useEffectEvent
React 19.2 adds useEffectEvent. It returns an event function that always sees the latest props and state, and it is not part of the dependency array. You define it and call it from inside an effect.
const onConnected = useEffectEvent(() => {
showToast('Connected to ' + theme);
});
useEffect(() => {
const connection = createConnection(roomId);
connection.on('connected', onConnected);
connection.connect();
return () => connection.disconnect();
}, [roomId]);
Only roomId is a dependency. The event reads the latest theme without forcing a reconnect.
Rules
useEffectEvent is not a general replacement for useCallback. The returned function is not stable and must not be passed to other components or used as a dependency. It can only be called from inside an effect. If you need a stable callback for a child, use useCallback.
Pitfall: calling it outside an effect
Calling an effect event from an event handler or during render is not supported and React warns. If the logic runs on a user event, it belongs in the handler, not in an effect event.
When to reach for it
Use it when an effect must read the latest value but that value should not restart the effect. It replaces the older workaround of a ref that mirrors the latest value.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
When theme changes, does the connection effect re-run?
Read the code, then choose the best answer. The reference explanation stays hidden until you respond. stimulus.tsxconst onConnected = useEffectEvent(() => log(theme)); useEffect(() => { const c = connect(roomId); c.on('open', onConnected); return () => c.close(); }, [roomId]);Exercise 2Implement
Refactor this effect so it reads the latest onTick without re-subscribing on every render.
starter.tsxuseEffect(() => { const id = setInterval(() => onTick(count), 1000); return () => clearInterval(id); }, [onTick, count]);Reveal the reference answer
reference.tsxconst handleTick = useEffectEvent(() => onTick(count)); useEffect(() => { const id = setInterval(handleTick, 1000); return () => clearInterval(id); }, []);Authored expected output: The interval is created once and always calls the latest onTick with the latest count.
The effect event reads the latest onTick and count without being a dependency, so the interval is not recreated on every render. The empty array is now correct.
Exercise 3Debug
This code warns that an effect event was called outside an effect. Explain and fix.
starter.tsxconst onClick = useEffectEvent(() => save(draft)); return <button onClick={onClick}>Save</button>;Reveal the reference answer
reference.tsxconst onClick = useCallback(() => save(draft), [draft]); return <button onClick={onClick}>Save</button>;Authored expected output: The save runs from the click handler with no warning.
Effect events are only valid inside effects. Logic that runs on a user event belongs in a normal handler or a useCallback, not in useEffectEvent.
Exercise 4Explain
Explain the problem useEffectEvent solves, using the dependency-array trade-off.
starter.tsx// theme in deps: reconnects. theme omitted: stale.Reveal the reference answer
reference.tsxAn effect often mixes reactive values that should restart it with non-reactive values that should be read fresh. Adding the non-reactive value to the dependency array restarts the effect unnecessarily; omitting it captures a stale value. useEffectEvent gives you a function that reads the latest values without becoming a dependency, so the effect restarts only for the values that truly matter.Authored expected output: A written explanation of reactive versus non-reactive values and the stale-versus-restart trade-off.
The hook separates what should trigger the effect from what the effect should read. That removes both the stale-value bug and the over-triggering bug.
Common misconceptions
#- useEffectEvent is a stable callback like useCallback. It is not stable and must not be passed around or used as a dependency.
- You can call an effect event anywhere. It is only valid inside an effect.
- useEffectEvent replaces useEffect. It is a helper for non-reactive logic inside an effect.
- The event function must be listed in the dependency array. It is intentionally excluded from dependencies.
Follow-up practice
#- react-external-stores
- react-custom-hooks
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.