Skip to content

Instantly share code, notes, and snippets.

@n4bb12
Created June 3, 2026 09:58
Show Gist options
  • Select an option

  • Save n4bb12/979cfe64dec7f5fd440475d732bb9f50 to your computer and use it in GitHub Desktop.

Select an option

Save n4bb12/979cfe64dec7f5fd440475d732bb9f50 to your computer and use it in GitHub Desktop.
useEventCallback
import { useCallback, useRef } from "react"
/**
* Stable callback identity with always-fresh implementation.
* @see https://github.com/facebook/react/issues/14099
*/
export function useEventCallback<F extends (...args: never[]) => unknown>(fn: F): F {
const ref = useRef(fn)
ref.current = fn
return useCallback(((...args) => ref.current.apply(undefined, args)) as F, [])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment