Skip to content

Commit e2cdb94

Browse files
committed
fix: useThrottle & useThrottleFn proper timeout type;
1 parent 943acb2 commit e2cdb94

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

src/useThrottle.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import useUnmount from './useUnmount';
33

44
const useThrottle = <T>(value: T, ms: number = 200) => {
55
const [state, setState] = useState<T>(value);
6-
const timeout = useRef<any>(null);
6+
const timeout = useRef<ReturnType<typeof setTimeout>>();
77
const nextValue = useRef(null) as any;
88
const hasNextValue = useRef(0) as any;
99

@@ -16,7 +16,7 @@ const useThrottle = <T>(value: T, ms: number = 200) => {
1616
setState(nextValue.current);
1717
timeout.current = setTimeout(timeoutCallback, ms);
1818
} else {
19-
timeout.current = null;
19+
timeout.current = undefined;
2020
}
2121
};
2222
timeout.current = setTimeout(timeoutCallback, ms);
@@ -27,7 +27,7 @@ const useThrottle = <T>(value: T, ms: number = 200) => {
2727
}, [value]);
2828

2929
useUnmount(() => {
30-
clearTimeout(timeout.current);
30+
timeout.current && clearTimeout(timeout.current);
3131
});
3232

3333
return state;

src/useThrottleFn.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import useUnmount from './useUnmount';
33

44
const useThrottleFn = <T>(fn: (...args: any[]) => T, ms: number = 200, args: any[]) => {
55
const [state, setState] = useState<T>(null as any);
6-
const timeout = useRef<any>(null);
6+
const timeout = useRef<ReturnType<typeof setTimeout>>();
77
const nextArgs = useRef(null) as any;
88
const hasNextArgs = useRef(false) as any;
99

@@ -16,7 +16,7 @@ const useThrottleFn = <T>(fn: (...args: any[]) => T, ms: number = 200, args: any
1616
setState(fn(...nextArgs.current));
1717
timeout.current = setTimeout(timeoutCallback, ms);
1818
} else {
19-
timeout.current = null;
19+
timeout.current = undefined;
2020
}
2121
};
2222
timeout.current = setTimeout(timeoutCallback, ms);
@@ -27,7 +27,7 @@ const useThrottleFn = <T>(fn: (...args: any[]) => T, ms: number = 200, args: any
2727
}, args);
2828

2929
useUnmount(() => {
30-
clearTimeout(timeout.current);
30+
timeout.current && clearTimeout(timeout.current);
3131
});
3232

3333
return state;

0 commit comments

Comments
 (0)