import {useCallback, useRef} from 'react'; import {EMPTY_ARRAY} from 'default-values'; type AnyFunc = (...params: any[]) => any; // 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: T, ): T extends AnyFunc ? T : () => void => { const ref = useRef(callback); ref.current = callback; return useCallback( (...params: any[]) => ref.current?.(...params), EMPTY_ARRAY, // eslint-disable-line react-hooks/exhaustive-deps ); }; export default useStableCallback;