From 284d59ae5401715181c8cc6e330a6128cfcbc401 Mon Sep 17 00:00:00 2001 From: wupanyan Date: Mon, 31 Jul 2023 12:45:52 +0800 Subject: [PATCH] fix(useLockFn): ensure the stability of the output function references of useLockFn --- packages/hooks/src/useLockFn/index.ts | 29 ++++++++++++--------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/packages/hooks/src/useLockFn/index.ts b/packages/hooks/src/useLockFn/index.ts index 5ce96fba40..a1831102a7 100644 --- a/packages/hooks/src/useLockFn/index.ts +++ b/packages/hooks/src/useLockFn/index.ts @@ -1,23 +1,20 @@ import { useRef, useCallback } from 'react'; - +import useMemoizedFn from '../useMemoizedFn'; function useLockFn

(fn: (...args: P) => Promise) { const lockRef = useRef(false); - return useCallback( - async (...args: P) => { - if (lockRef.current) return; - lockRef.current = true; - try { - const ret = await fn(...args); - lockRef.current = false; - return ret; - } catch (e) { - lockRef.current = false; - throw e; - } - }, - [fn], - ); + return useMemoizedFn(async (...args: P) => { + if (lockRef.current) return; + lockRef.current = true; + try { + const ret = await fn(...args); + lockRef.current = false; + return ret; + } catch (e) { + lockRef.current = false; + throw e; + } + }); } export default useLockFn;