useEffectEvent

useEffectEvent is a React Hook that lets you separate events from Effects.

const onEvent = useEffectEvent(callback)

Reference

useEffectEvent(callback)

Call useEffectEvent at the top level of your component to create an Effect Event.

import { useEffectEvent, useEffect } from 'react';

function ChatRoom({ roomId, theme }) {
const onConnected = useEffectEvent(() => {
showNotification('Connected!', theme);
});
}

Effect Events are a part of your Effect logic, but they behave more like an event handler. They always “see” the latest values from render (like props and state) without re-synchronizing your Effect, so they’re excluded from Effect dependencies. See Separating Events from Effects to learn more.

See more examples below.

Parameters

  • callback: A function containing the logic for your Effect Event. The function can accept any number of arguments and return any value. When you call the returned Effect Event function, the callback always accesses the latest committed values from render at the time of the call.

Returns

useEffectEvent returns an Effect Event function with the same type signature as your callback.

You can call this function inside useEffect, useLayoutEffect, useInsertionEffect, or from within other Effect Events in the same component.

Caveats

  • useEffectEvent is a Hook, so you can only call it at the top level of your component or your own Hooks. You can’t call it inside loops or conditions. If you need that, extract a new component and move the Effect Event into it.
  • Effect Events can only be called from inside Effects or other Effect Events. Do not call them during rendering or pass them to other components or Hooks. The eslint-plugin-react-hooks linter enforces this restriction.
  • Do not use useEffectEvent to avoid specifying dependencies in your Effect’s dependency array. This hides bugs and makes your code harder to understand. Only use it for logic that is genuinely an event fired from Effects.
  • Effect Event functions do not have a stable identity. Their identity intentionally changes on every render.
Derinlemesine İnceleme

Why are Effect Events not stable?

Unlike set functions from useState or refs, Effect Event functions do not have a stable identity. Their identity intentionally changes on every render:

// 🔴 Wrong: including Effect Event in dependencies
useEffect(() => {
onSomething();
}, [onSomething]); // ESLint will warn about this

This is a deliberate design choice. Effect Events are meant to be called only from within Effects in the same component. Since you can only call them locally and cannot pass them to other components or include them in dependency arrays, a stable identity would serve no purpose, and would actually mask bugs.

The non-stable identity acts as a runtime assertion: if your code incorrectly depends on the function identity, you’ll see the Effect re-running on every render, making the bug obvious.

This design reinforces that Effect Events conceptually belong to a particular effect, and are not a general purpose API to opt-out of reactivity.


Usage

Using an event in an Effect

Call useEffectEvent at the top level of your component to create an Effect Event:

const onConnected = useEffectEvent(() => {
if (!muted) {
showNotification('Connected!');
}
});

useEffectEvent accepts an event callback and returns an Effect Event. The Effect Event is a function that can be called inside of Effects without re-connecting the Effect:

useEffect(() => {
const connection = createConnection(roomId);
connection.on('connected', onConnected);
connection.connect();
return () => {
connection.disconnect();
}
}, [roomId]);

Since onConnected is an Effect Event, muted and onConnect are not in the Effect dependencies.

Tuzak

Don’t use Effect Events to skip dependencies

It might be tempting to use useEffectEvent to avoid listing dependencies that you think are “unnecessary.” However, this hides bugs and makes your code harder to understand:

// 🔴 Wrong: Using Effect Events to hide dependencies
const logVisit = useEffectEvent(() => {
log(pageUrl);
});

useEffect(() => {
logVisit()
}, []); // Missing pageUrl means you miss logs

If a value should cause your Effect to re-run, keep it as a dependency. Only use Effect Events for logic that genuinely should not re-trigger your Effect.

See Separating Events from Effects to learn more.