@hypothesis/frontend-shared
Version:
Shared components, styles and utilities for Hypothesis projects
21 lines (19 loc) • 741 B
JavaScript
import { useRef } from 'preact/hooks';
/**
* Return a function which wraps a callback to give it a stable value.
*
* The wrapper has a stable value across renders, but always forwards to the
* callback from the most recent render. This is useful if you want to use a
* callback inside a `useEffect` or `useMemo` hook without re-running the effect
* or re-computing the memoed value when the callback changes.
*/
export function useStableCallback(callback) {
const wrapper = useRef({
callback,
call: (...args) => wrapper.current.callback(...args)
});
// On each render, save the last callback value.
wrapper.current.callback = callback;
return wrapper.current.call;
}
//# sourceMappingURL=use-stable-callback.js.map