DEV Community

Cover image for React useEffectEvent: Fix "Is Not a Function", Lint Errors and useCallback Confusion
ByteOrbit
ByteOrbit

Posted on Originally published at byteorbit.tech

React useEffectEvent: Fix "Is Not a Function", Lint Errors and useCallback Confusion

React useEffectEvent: Fix "Is Not a Function", Lint Errors and useCallback Confusion

useEffectEvent is the hook React added in 19.2 so an Effect can read the latest props and state without re-running every time they change. This guide to React useEffectEvent covers the part that sends most people to a search box: why you get useEffectEvent is not a function, why ESLint now complains about your dependency array, and how it differs from useCallback. The behaviours below were measured on our machine, and where we quote React we say so.

Tested with React 19.2.0 and react-dom 19.2.0 in jsdom, eslint-plugin-react-hooks 7.1.1 on ESLint 9.39.5, and Node 24.22.1. We also ran the same component on React 19.1.0 to reproduce the error, and we opened the React copy that ships inside two Next.js tarballs.

What React useEffectEvent does

An Effect has two kinds of values in it. Some describe what the Effect synchronizes with (a roomId you connect to). Others are only read when something happens inside the Effect (the theme you show in a toast after connecting). Before 19.2 you had to list both in the dependency array, so a theme change tore down and rebuilt the connection.

import { useEffect, useEffectEvent } from 'react';

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

  useEffect(() => {
    const connection = createConnection(serverUrl, roomId);
    connection.on('connected', () => onConnected());
    connection.connect();
    return () => connection.disconnect();
  }, [roomId]); // theme and onConnected are not dependencies
}
Enter fullscreen mode Exit fullscreen mode

onConnected always runs the callback from the latest committed render, like a DOM event handler would. The Effect only re-runs when roomId changes.

The measured difference

We rendered a component twice with the same roomId and a new theme (light, then dark). The Effect starts a 15 ms timer and then calls a handler that records the theme it saw. Three versions of the handler:

Handler Effect runs Theme the handler saw
useEffectEvent, deps [roomId] 1 dark (latest)
useCallback with [theme], put in deps 2 dark (but the Effect restarted)
useCallback with [] 1 light (stale closure)

Only the first row gets both properties you want: no restart and no stale value. The second row is correct but reconnects on every theme change. The third row is the classic bug where someone silences the lint rule.

React useEffectEvent is not a function: the fix

The error text is useEffectEvent is not a function (or React.useEffectEvent is not a function). It means the React that actually loads at runtime is older than 19.2. We reproduced it on 19.1.0:

node -e "const R=require('react');R.useEffectEvent(()=>1)"
# R.useEffectEvent is not a function
Enter fullscreen mode Exit fullscreen mode

With a native ES module import the same problem shows up earlier, as SyntaxError: Named export 'useEffectEvent' not found. Bundlers usually turn it into the runtime TypeError instead.

Work through these checks in order:

  1. Run npm ls react react-dom (or pnpm why react). You want exactly one copy of each, and both on 19.2.0 or later. A monorepo with a second copy of React 19.1 in a nested package is the most common cause.
  2. Update react and react-dom together. Their versions should match.
  3. If you use Next.js, check the Next version too. Next ships its own pre-compiled React and aliases react to it, so the version in your package.json is not the one your app uses. We opened dist/compiled/react/cjs/react.production.js in the published tarballs: Next 15.4.6 contains no reference to useEffectEvent, while Next 16.4.6 does. If you are on an older Next 15, upgrade Next, not only React. The upstream issue for this is vercel/next.js 84462.
  4. Clear the bundler cache and reinstall if the lockfile still pins an older React.

React 18 has no useEffectEvent. The hook is not available there, so upgrade to React 19.2 or keep a small latest-value ref until you can. If you are planning that jump, our React 19 useActionState guide covers another React 19 API that is worth adopting at the same time.

// React 18 fallback: a ref that always holds the latest callback
function useLatest(fn) {
  const ref = useRef(fn);
  useLayoutEffect(() => { ref.current = fn; });
  return useCallback((...args) => ref.current(...args), []);
}
Enter fullscreen mode Exit fullscreen mode

This works, but it is hand-rolled and the linter does not know about it, so you lose the checks described below.

useEffectEvent vs useCallback

They look similar and solve different problems.

useEffectEvent useCallback
Purpose Read latest values inside an Effect without re-running it Keep a function identity stable between renders
Identity across renders Changes on every render (we checked: not equal) Stable until dependencies change
In dependency arrays Must not be listed Usually listed
Pass to child components No Yes
Call during render Throws Allowed

We confirmed two rows directly. Comparing the function from two renders gave false, which is by design: React's reference page says Effect Event identity "intentionally changes on every render", so that anything misusing it is caught. Calling one during render throws A function wrapped in useEffectEvent can't be called during rendering.

Use useCallback when a child is wrapped in memo and needs a stable prop. Use useEffectEvent when an Effect needs fresh values for something that is event-like. If you are adopting the compiler as well, remember the compiler memoizes values and components while this hook controls Effect dependencies; see React Compiler in Vite 8 for that side of the setup.

The ESLint messages you will see

eslint-plugin-react-hooks understands Effect Events. The React team's release notes say to upgrade to the latest plugin so it stops asking you to add the Effect Event to the array. With version 7.1.1 and the flat recommended config, this file:

const onConnected = useEffectEvent(() => { console.log(theme); });
useEffect(() => { onConnected(); }, [roomId, onConnected]);
const handler = () => onConnected();
Enter fullscreen mode Exit fullscreen mode

produced two reports:

4:48  warning  Functions returned from `useEffectEvent` must not be included in the dependency array. Remove `onConnected` from the list   react-hooks/exhaustive-deps
5:25  error    `onConnected` is a function created with React Hook "useEffectEvent", and can only be called from Effects and Effect Events in the same component   react-hooks/rules-of-hooks
Enter fullscreen mode Exit fullscreen mode

Fix the first by deleting onConnected from the array. Fix the second by calling the Effect Event only inside an Effect (or another Effect Event) in the same component. Passing it to a child or calling it from a click handler is not allowed, and the click handler should simply contain the logic itself.

If the plugin still asks you to add the Effect Event as a dependency, you are on an old plugin. Upgrade eslint-plugin-react-hooks to a 6.1 or later release. The recommended preset switched to flat config in version 6, and the legacy preset is named recommended-legacy.

When not to use useEffectEvent

React's docs are blunt about this: do not use it just to make the dependency warning go away. If a value is part of what the Effect synchronizes with, it belongs in the array. Wrapping a fetch URL in an Effect Event, for example, makes the data go stale when the URL changes, and nothing will warn you.

A quick test: would a user perceive a change in this value as "something new to synchronize"? A roomId or a search query, yes. A theme toast or an analytics label, no. Only the second kind is an Effect Event.

Other cases that should not use it: logic that reacts to a form submit (put it in the action, as in our useActionState guide), and values you want in a child component. For the validation side of forms, our post on Zod 4 error messages covers how to turn schema issues into field messages.

Practical rules we ended up with

  • Declare the Effect Event in the same component or custom Hook as the Effect that calls it.
  • Name it for what happens (onConnected, onMessage), not for the effect.
  • Keep it small. If the callback is long, it probably contains logic that should be a separate function called from both places.
  • Do not return it from a custom Hook. Return a regular function or move the Effect into the Hook.
  • Test the behaviour you care about: a rerender with a changed prop should not restart the subscription.

FAQ

Why is useEffectEvent not a function in my React 19 app?

The runtime React is older than 19.2. Check npm ls react react-dom for a second copy, update both packages together, and on Next.js upgrade Next itself because it aliases React to its own bundled copy. Next 15.4.6 ships a React without useEffectEvent, Next 16.4.6 includes it.

Is useEffectEvent available in React 18?

No. It first appeared as a stable export in React 19.2. On React 18 use a latest-value ref wrapper, or upgrade.

Should I put the Effect Event in the dependency array?

No. React and the linter both say it must not be listed. Its identity changes every render, so listing it would re-run the Effect on every render, which is exactly what the hook exists to prevent.

What is the difference between useEffectEvent and useCallback?

useCallback keeps a function identity stable for children and dependency arrays. useEffectEvent makes a function that always sees the latest render values, is excluded from dependencies, and may only be called from Effects.

Does useEffectEvent replace useRef for latest values?

For event-like logic called from an Effect, yes, and it is checked by the linter. For values you need to read during render or share across components, you still want state or refs.


Official docs: useEffectEvent reference and the React 19.2 release post.

Originally published on ByteOrbit.

Top comments (0)