@rozhkov/react-useful-hooks
Version:
Useful hooks for React application
13 lines (12 loc) • 656 B
JavaScript
import { useCallback, useRef } from 'react';
import { EMPTY_ARRAY } from 'default-values';
// Utility hook that returns a function that never has stale dependencies, but
// without changing identity, as a useCallback with dep array would.
// Useful for functions that depend on external state, but
// should not trigger effects when that external state changes.
const useStableCallback = (callback) => {
const ref = useRef(callback);
ref.current = callback;
return useCallback((...params) => { var _a; return (_a = ref.current) === null || _a === void 0 ? void 0 : _a.call(ref, ...params); }, EMPTY_ARRAY);
};
export default useStableCallback;