From e2cdb94c9b432cf7a4d600571b3c96837b59c5f0 Mon Sep 17 00:00:00 2001 From: xobotyi Date: Mon, 26 Aug 2019 00:33:22 +0300 Subject: [PATCH] fix: useThrottle & useThrottleFn proper timeout type; --- src/useThrottle.ts | 6 +++--- src/useThrottleFn.ts | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/useThrottle.ts b/src/useThrottle.ts index 38aaed0429..b231ba626d 100644 --- a/src/useThrottle.ts +++ b/src/useThrottle.ts @@ -3,7 +3,7 @@ import useUnmount from './useUnmount'; const useThrottle = (value: T, ms: number = 200) => { const [state, setState] = useState(value); - const timeout = useRef(null); + const timeout = useRef>(); const nextValue = useRef(null) as any; const hasNextValue = useRef(0) as any; @@ -16,7 +16,7 @@ const useThrottle = (value: T, ms: number = 200) => { setState(nextValue.current); timeout.current = setTimeout(timeoutCallback, ms); } else { - timeout.current = null; + timeout.current = undefined; } }; timeout.current = setTimeout(timeoutCallback, ms); @@ -27,7 +27,7 @@ const useThrottle = (value: T, ms: number = 200) => { }, [value]); useUnmount(() => { - clearTimeout(timeout.current); + timeout.current && clearTimeout(timeout.current); }); return state; diff --git a/src/useThrottleFn.ts b/src/useThrottleFn.ts index 9c749855b8..fafbe7b883 100644 --- a/src/useThrottleFn.ts +++ b/src/useThrottleFn.ts @@ -3,7 +3,7 @@ import useUnmount from './useUnmount'; const useThrottleFn = (fn: (...args: any[]) => T, ms: number = 200, args: any[]) => { const [state, setState] = useState(null as any); - const timeout = useRef(null); + const timeout = useRef>(); const nextArgs = useRef(null) as any; const hasNextArgs = useRef(false) as any; @@ -16,7 +16,7 @@ const useThrottleFn = (fn: (...args: any[]) => T, ms: number = 200, args: any setState(fn(...nextArgs.current)); timeout.current = setTimeout(timeoutCallback, ms); } else { - timeout.current = null; + timeout.current = undefined; } }; timeout.current = setTimeout(timeoutCallback, ms); @@ -27,7 +27,7 @@ const useThrottleFn = (fn: (...args: any[]) => T, ms: number = 200, args: any }, args); useUnmount(() => { - clearTimeout(timeout.current); + timeout.current && clearTimeout(timeout.current); }); return state;