diff --git a/components/_util/hooks/usePersistCallback.ts b/components/_util/hooks/usePersistCallback.ts new file mode 100644 index 0000000000..3242392065 --- /dev/null +++ b/components/_util/hooks/usePersistCallback.ts @@ -0,0 +1,16 @@ +import { useRef, useCallback } from 'react'; + +export default function usePersistCallback any>(fn: T) { + const ref = useRef(); + + ref.current = fn; + + return useCallback( + // @ts-ignore + (...args) => { + const fn = ref.current; + return fn && fn(...args); + }, + [ref] + ); +}