Skip to main content

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.

One Effect, two different kinds of values
REACTIVE CONFIGURATIONroomId

Configures which external connection exists. Changing it should reconnect.

LATEST VALUEtheme

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]);
}
Reactive restart vs latest-value read
roomId changes
Reconnect
theme changes
Next Effect Event call sees latest theme

Mental model​

What is an Effect Event?
Logic belongs to an Effect
It needs the latest committed values
Those values should not restart synchronization

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]);
}
Timer ownership
intervalMs
Configures the timer, so changing it restarts the timer.
message
Does not configure the timer; each tick should read the latest message.

Classify reactive and non-reactive logic​

Ask one question:

Should changing this value restart or reconfigure the external system?

How should an Effect-related value behave?
Changing it must restart/reconfigure the external processKeep it as a normal Effect dependency
Changing it should not restart, but the latest value is needed when an external event firesConsider reading it inside an Effect Event
The work is caused directly by a click, submit, or inputUse a normal event handler instead
The value is needed to calculate UIRead it during render

Examples:

ValueLikely role
WebSocket URLReactive connection configuration
roomIdReactive connection configuration
Notification themeLatest-value Effect Event logic
Muted preferenceOften latest-value event logic
Latest cart count for loggingPossibly 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]);
Effect Event misuse
WRONGHide a real dependency
Moves reactive configuration into an Effect Event merely to silence the linter.
RIGHTSeparate roles honestly
Dependencies restart the process; Effect Events read non-reactive latest values.

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​

Different tools, different problems
useCallback
Memoizes function identity when identity itself matters.
useEffectEvent
Separates non-reactive Effect logic and reads latest committed values.

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]);
}
Listener lifecycle and latest callback
1enabled controls listener existence
2Browser keydown event fires
3Effect Event runs
4Latest onShortcut callback is used

When should you use it?​

Use useEffectEvent when all are true:

  1. the logic genuinely belongs to an Effect;
  2. it needs the latest committed values;
  3. those values should not restart the external system;
  4. 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​

Effect Event debugging flow
1Name the external system
2Identify values that configure it
3Keep those as dependencies
4Identify latest-only values used when events fire
5Move only those reads into an Effect Event

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​

Reactive Effect logic vs Effect Event logic
Reactive Effect logic
Determines when synchronization restarts.
Effect Event logic
Runs from an Effect and sees latest committed values without becoming a restart dependency.

References​

Next​

Continue with useRef.