-
Matiss Janis Aboltins authored
Moving the code to separate files. Functionally should be no differences.
Matiss Janis Aboltins authoredMoving the code to separate files. Functionally should be no differences.
useStableCallback.ts 467 B
import { useRef, useLayoutEffect, useCallback } from 'react';
type UseStableCallbackArg = (...args: unknown[]) => unknown;
export default function useStableCallback(callback: UseStableCallbackArg) {
const callbackRef = useRef<UseStableCallbackArg>();
const memoCallback = useCallback(
(...args) => callbackRef.current && callbackRef.current(...args),
[],
);
useLayoutEffect(() => {
callbackRef.current = callback;
});
return memoCallback;
}