From 4295b2e1b82772d289127f0724cb912e3dc420f8 Mon Sep 17 00:00:00 2001 From: rogerleung0411 Date: Wed, 22 May 2024 21:29:45 +0800 Subject: [PATCH] feat: add usePersistCallback --- components/_util/hooks/usePersistCallback.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 components/_util/hooks/usePersistCallback.ts 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] + ); +}