Skip to content
Tutorials Center
My learning

Topics

React Reference ยท Effect and Ref Hooks

React useEffectEvent hook

Reference for React useEffectEvent: read the latest props and state inside an effect without making them dependencies, plus the rules and limits.

useEffectEvent lets you pull "event-like" logic out of an effect. The function it returns always sees the newest props and state from the last committed render, yet it is not a reactive value, so you do not list it in the effect's dependency array. Use it for code that must run because of the effect but should not restart the effect when the values it reads change.

The hook was added in React 19.2. It is meant to be called only from inside effects (and from other Effect Events), never during render and never passed down to other components.

JSX
const onConnected = useEffectEvent(() => {
  console.log('Connected, theme is ' + theme);
});

Try it

Latest theme without reconnecting

Press "Toggle theme" right after the page loads: no "disconnecting" line appears, but when the timer fires the message mentions the theme you just chose.

Logging a page visit with the current cart

Adding items does not log anything. Only navigating logs, and each visit line carries the cart size at that moment.

Syntax

JSX
import { useEffectEvent } from 'react';

const fn = useEffectEvent(callback);
// TypeScript: useEffectEvent<T extends (...args: any[]) => any>(callback: T): T
Name Description
callback A function that reads props, state or other reactive values. It may take parameters and return a value.

Returns: a function with the same signature as callback. Its identity is intentionally not stable, so never put it in a dependency array.

Errors / edge cases: calling the returned function while React is rendering throws an error in development. Passing it to a child component or storing it for later use outside an effect is unsupported.

Usage notes

  • Use it for events, not for sync logic. If a value should restart the effect (a room id, a URL, a user id), keep it as a dependency. Move only the "read it at this moment" part into the Effect Event.
  • It sees the last committed render. It does not see values from a render that has not committed yet.
  • Call it only from effects. Calling from a normal event handler is pointless (handlers already see fresh values), and calling during render is an error.
  • Not a performance tool. Do not reach for it to dodge dependencies on values that really should re-run the effect; that hides bugs, much like silencing the linter would.
  • Linting. eslint-plugin-react-hooks knows about Effect Events: they are exempt from the dependency rule, but the plugin flags using them outside effects.
  • Before 19.2 the usual workaround was a ref updated in an effect. useEffectEvent replaces that pattern.
  • Unlike useCallback, it does not memoize for children; it exists only to be called from effects.