A callback with a stable identity that always runs the latest closure — like useCallback,
except that a change in deps does NOT change the returned function. Pass it to memoized
children, effect dependency lists or long-lived subscriptions (a command contribution's data,
a model listener) without re-running them whenever the closure's inputs change.
This is the "latest ref" pattern (React's useEvent RFC; ahooks' useMemoizedFn; Radix's
useCallbackRef).
Usage notes:
List every value the callback reads in deps. The closure is only refreshed when deps
change, so a missing dependency means the stable function calls a stale closure.
Call it from events and deferred work, not during render. Its identity never changes, so
a component that reads its result while rendering is not re-rendered when the inputs change.
For logic called only from an effect, React's own useEffectEvent (React 19.2+) is the
standard alternative. It cannot be passed to children or called during render, which this hook
allows.
A callback with a stable identity that always runs the latest closure — like
useCallback, except that a change indepsdoes NOT change the returned function. Pass it to memoized children, effect dependency lists or long-lived subscriptions (a command contribution'sdata, a model listener) without re-running them whenever the closure's inputs change.This is the "latest ref" pattern (React's
useEventRFC; ahooks'useMemoizedFn; Radix'suseCallbackRef).Usage notes:
deps. The closure is only refreshed whendepschange, so a missing dependency means the stable function calls a stale closure.useEffectEvent(React 19.2+) is the standard alternative. It cannot be passed to children or called during render, which this hook allows.