Skip to Content
ohooks

useEventCallback

Returns a referentially stable callback that always invokes the latest version of fn.

Loading preview...

The useEventCallback hook wraps a function in a stable callback reference whose identity never changes across renders while always reading the latest props and state when invoked.

Installation

pnpm dlx shadcn@latest add https://ohooks.vercel.app/r/useEventCallback.json

Usage

Examples

Passing Stable Callbacks to Memoized Children

Pass the returned function to React.memo child components so they do not re-render when parent state changes.

Avoiding Effect Re-Subscriptions

Call an event callback inside useEffect without adding rapidly changing dependencies that would tear down and re-create a subscription.