useEventCallback
Creates a callback with a stable identity that always delegates to the most
recent version of the received function. Lets you register handlers on
external listeners (window events, integrations with imperative libraries)
without suffering from stale closures and without invalidating deps.
Also exports useIsomorphicEffect, an SSR-safe version of useLayoutEffect.
When to use
| ✅ Use when… | 🚫 Avoid when… |
|---|---|
|
|
Signature
const stable = useEventCallback(fn: (...args: any[]) => void);Example
import { useEventCallback } from '@apollion-dsi/core/hooks';
import { useEffect } from 'react';
function Component({ onResize }: { onResize: () => void }) {
const handler = useEventCallback(onResize);
useEffect(() => {
window.addEventListener('resize', handler);
return () => window.removeEventListener('resize', handler);
}, [handler]); // stable identity; the effect runs only once
}See also
- Full API:
useEventCallback(reference generated from TSDoc).