useEffectEvent
React 19.2+
useEffectEvent separates reactive synchronization from non-reactive logic that runs as part of an Effect.
import {useEffect, useEffectEvent} from 'react';
The reconnect problem
function ChatRoom({roomId, theme}) {
useEffect(() => {
const connection = createConnection(roomId);
connection.on('connected', () => {
showNotification('Connected!', theme);
});
connection.connect();
return () => connection.disconnect();
}, [roomId, theme]);
}
roomId and theme play different roles.
Configures which external connection exists. Changing it should reconnect.
Only affects notification presentation. Changing it should not reconnect.
Putting both values in the dependency list makes a theme change reconnect the chat unnecessarily.
Effect Event solution
function ChatRoom({roomId, theme}) {
const onConnected = useEffectEvent(() => {
showNotification('Connected!', theme);
});
useEffect(() => {
const connection = createConnection(roomId);
connection.on('connected', () => {
onConnected();
});
connection.connect();
return () => connection.disconnect();
}, [roomId]);
}
Mental model
It behaves somewhat like an event handler, but it is specifically for logic called from Effects.
Basic syntax
const onSomething = useEffectEvent(() => {
// read latest committed props/state here
});
Call it from an Effect:
useEffect(() => {
onSomething();
}, [/* actual synchronization dependencies */]);
Effect Events see latest committed values
function Timer({intervalMs, message}) {
const onTick = useEffectEvent(() => {
console.log(message);
});
useEffect(() => {
const id = setInterval(() => {
onTick();
}, intervalMs);
return () => clearInterval(id);
}, [intervalMs]);
}
Classify reactive and non-reactive logic
Ask one question:
Should changing this value restart or reconfigure the external system?
Examples:
| Value | Likely role |
|---|---|
| WebSocket URL | Reactive connection configuration |
roomId | Reactive connection configuration |
| Notification theme | Latest-value Effect Event logic |
| Muted preference | Often latest-value event logic |
| Latest cart count for logging | Possibly latest-value event logic |
The correct classification depends on the external system being synchronized.
Do not use Effect Events to cheat dependencies
Bad:
const run = useEffectEvent(() => {
connect(roomId);
});
useEffect(() => {
run();
}, []); // ❌ hides that the connection depends on roomId
roomId configures the connection. It belongs in normal Effect synchronization:
useEffect(() => {
const connection = connect(roomId);
return () => connection.disconnect();
}, [roomId]);
Where Effect Events can be called
They are for Effect logic and may be called from:
useEffect;useLayoutEffect;useInsertionEffect;- another Effect Event in the same component.
Do not call them during rendering.
const readLatest = useEffectEvent(() => value);
const current = readLatest(); // ❌ during render
If render needs value, read value directly.
Do not pass Effect Events around
<Child onConnected={onConnected} /> // ❌
Effect Events conceptually belong to the Effect that uses them. A child callback triggered by user interaction should be a normal event callback.
Do not put Effect Events in dependency arrays
useEffect(() => {
onConnected();
}, [onConnected]); // ❌
Effect Event functions intentionally do not provide stable identity and are not dependencies.
Effect Event vs useCallback
useEffectEvent is not a performance optimization.
Effect Event vs a ref for latest values
Older code sometimes stores the latest value manually:
const themeRef = useRef(theme);
themeRef.current = theme;
useEffect(() => {
const connection = createConnection(roomId);
connection.on('connected', () => {
showNotification('Connected!', themeRef.current);
});
return () => connection.disconnect();
}, [roomId]);
For genuine Effect-event logic in React 19.2+, useEffectEvent communicates intent more clearly and integrates with the Hooks linter. Refs remain useful for other imperative, non-render values.
Browser listener example
function KeyboardTracker({enabled, onShortcut}) {
const handleShortcut = useEffectEvent(event => {
onShortcut(event.key);
});
useEffect(() => {
if (!enabled) return;
function handleKeyDown(event) {
if (event.ctrlKey && event.key === 'k') {
handleShortcut(event);
}
}
window.addEventListener('keydown', handleKeyDown);
return () => window.removeEventListener('keydown', handleKeyDown);
}, [enabled]);
}
When should you use it?
Use useEffectEvent when all are true:
- the logic genuinely belongs to an Effect;
- it needs the latest committed values;
- those values should not restart the external system;
- the logic is called from an Effect or another Effect Event.
Do not use it for normal user-event handlers, render calculations, dependency suppression, stable callback identity, cross-component callback props, or hiding genuine synchronization dependencies.
Debugging checklist
Production example
function Notifications({workspaceId, muted}) {
const onNotification = useEffectEvent(notification => {
if (!muted) {
showDesktopNotification(notification);
}
});
useEffect(() => {
const socket = connectToWorkspace(workspaceId);
socket.on('notification', onNotification);
socket.connect();
return () => socket.disconnect();
}, [workspaceId]);
}
Changing workspaces reconnects. Toggling mute does not reconnect, but later notifications use the latest mute preference.
Exercise
Refactor a player connection so changing trackId reconnects, changing volume does not reconnect, and the ready event uses the latest volume.
Interview questions
Junior: What problem does useEffectEvent solve?
Mid-level: Why is an Effect Event different from useCallback?
Senior: How would you decide whether a value belongs in the dependency list or should only be read from an Effect Event?
Summary
References
- https://react.dev/reference/react/useEffectEvent
- https://react.dev/learn/separating-events-from-effects
- https://react.dev/learn/removing-effect-dependencies
Next
Continue with useRef.