--- title: Non-reactive callbacks in React description: Patterns for reading the latest React callback without making an effect rerun when it changes. url: https://mattrossman.com/2024/09/30/non-reactive-callbacks-in-react/ image: https://mattrossman.com/open-graph/2024/09/30/non-reactive-callbacks-in-react.png --- # Non-reactive callbacks in React Sep 30, 2024 Sometimes React’s reactivity can be a bit trigger happy. Imagine you’re writing a hook or component that receives a callback as an argument. Maybe the owner of that callback memoized it to prevent excess re-renders. If they didn’t and you reference this callback in an effect, the effect will re-run on every render. Aside from being a performance issue, this can cause undesired behaviors. ``` function useWebSocket(onOpen) { /** * This could close/reopen a socket connection on every render */ useEffect(() => { const ws = new WebSocket("wss://example.com") ws.onopen = onOpen return () => { ws.close() } }, [onOpen]) } ``` There’s an [experimental useEffectEvent](https://19.react.dev/reference/react/experimental_useEffectEvent) hook for this purpose, [read more about it in the docs](https://react.dev/learn/separating-events-from-effects#reading-latest-props-and-state-with-effect-events). It started from [this (now outdated) RFC](https://github.com/reactjs/rfcs/pull/220) as useEvent and was later [renamed to useEffectEvent](https://github.com/facebook/react/pull/25881). I haven’t found an official polyfill for this, but there are some notable community attempts. Radix UI call it [useCallbackRef](https://github.com/radix-ui/primitives/blob/6469d41dcc6e84fe41d70a2703924338e7562dd1/packages/react/use-callback-ref/src/useCallbackRef.tsx), and BlueSky calls it [useNonReactiveCallback](https://github.com/bluesky-social/social-app/blob/587c0c625752964d8ce64faf1d329dce3c834a5c/src/lib/hooks/useNonReactiveCallback.ts). Sanity.io redistributes the BlueSky implementation under the [use-effect-event](https://github.com/sanity-io/use-effect-event) package as a useEffectEvent “ponyfill”. I also see a similar implementation in the [use-event-callback](https://github.com/Volune/use-event-callback/blob/0ad3b83d9ec46ebd59c704fecc5a706959932820/src/index.ts) package [mentioned in the useEvent RFC](https://github.com/reactjs/rfcs/blob/d85e257502a43c08d17e8ab58efa0880f7f007a5/text/0000-useevent.md#adoption-strategy) The gist of these approaches are: - Store the callback in a ref - Update this ref in an effect - (optional) Wrap it in a memoized function with useCallback or useMemo The last step is a convenience so you don’t have to read from `.current` every time you access the function. One consideration is *which* effect to use for syncing the callback. The Radix UI implementation uses `useEffect` while BlueSky uses `useInsertionEffect`, which runs before `useEffect` and `useLayoutEffect`. For my use case the timing subtleties don’t seem that critical but the old RFC indicates that [callbacks should be updated before effects run](https://github.com/reactjs/rfcs/blob/d85e257502a43c08d17e8ab58efa0880f7f007a5/text/0000-useevent.md#internal-implementation) to prevent reading stale values, so `useInsertionEffect` is the closest approximation. Dan Abromov’s comments indicate that these userland approximations [don’t perfectly solve the effect use case](https://github.com/facebook/react/pull/25881#issuecomment-1355334417), but I figure it’s the best we have for now. [He recommends using useInsertionEffect](https://github.com/facebook/react/pull/25881#issuecomment-1356244360) if possible (i.e. you’re using React 18) or [useLayoutEffect otherwise](https://github.com/facebook/react/pull/25881#issuecomment-1356315889). My preferred solution follows the official `useEffectEvent` semantics but follow Dan’s advice of syncing the ref as early as possible in an insertion effect. I’ll also prefer memoization with useCallback since useMemo doesn’t guarantee that cached values won’t be thrown away during the lifetime of a component (see [useMemo caveats](https://react.dev/reference/react/useMemo#caveats)). ``` import { useRef, useInsertionEffect, useCallback } from "react" function useEffectEvent(fn) { const ref = useRef(fn) useInsertionEffect(() => { ref.current = fn }, [fn]) return useCallback((...args) => ref.current?.(...args), []) } ``` TypeScript version ``` /* eslint-disable @typescript-eslint/no-unsafe-return */ /* eslint-disable @typescript-eslint/no-unsafe-argument */ /* eslint-disable @typescript-eslint/no-explicit-any */ import { useRef, useInsertionEffect, useCallback } from "react" export function useEffectEvent any) | undefined>( fn: T, ) { const ref = useRef(fn) useInsertionEffect(() => { ref.current = fn }, [fn]) return useCallback( (...args: any) => ref.current?.(...args), [], ) as T extends undefined ? () => void : T } ``` Sometimes in a pinch I’ll just use this quick 2-liner without a custom hook: ``` const callbackRef = useRef(callback) callbackRef.current = callback ``` Technically it’s not correct as explained in [this RFC comment](https://github.com/reactjs/rfcs/pull/220#issuecomment-1118027152) since concurrent features mean that when stuff is rendered by React isn’t always when it’s displayed. But then again, none of the userland approximations are fully correct. In any case it’s handy having a hook to turn this into a 1-liner and remove the need to read from `.current` when accessing the callback.