From be1b474f4175b98be4bb5b03d6e112f0795b8997 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 5 Sep 2023 09:29:00 +0200 Subject: [PATCH] Remove TimeoutToggle callback's dependency on initial value --- src/utils/helpers/hooks.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/utils/helpers/hooks.ts b/src/utils/helpers/hooks.ts index ded2cdbc..97fa7367 100644 --- a/src/utils/helpers/hooks.ts +++ b/src/utils/helpers/hooks.ts @@ -15,16 +15,17 @@ export const useTimeoutToggle = ( clearTimeout = window.clearTimeout, ): [boolean, () => void] => { const [flag, setFlag] = useState(initialValue); + const initialValueRef = useRef(initialValue); const timeout = useRef(undefined); const callback = useCallback(() => { - setFlag(!initialValue); + setFlag(!initialValueRef.current); if (timeout.current) { clearTimeout(timeout.current); } - timeout.current = setTimeout(() => setFlag(initialValue), delay); - }, [clearTimeout, delay, initialValue, setTimeout]); + timeout.current = setTimeout(() => setFlag(initialValueRef.current), delay); + }, [clearTimeout, delay, setTimeout]); return [flag, callback]; };