Skip to content

Commit

Permalink
fix: useThrottle & useThrottleFn proper timeout type;
Browse files Browse the repository at this point in the history
  • Loading branch information
xobotyi committed Aug 25, 2019
1 parent 943acb2 commit e2cdb94
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/useThrottle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import useUnmount from './useUnmount';

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

Expand All @@ -16,7 +16,7 @@ const useThrottle = <T>(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);
Expand All @@ -27,7 +27,7 @@ const useThrottle = <T>(value: T, ms: number = 200) => {
}, [value]);

useUnmount(() => {
clearTimeout(timeout.current);
timeout.current && clearTimeout(timeout.current);
});

return state;
Expand Down
6 changes: 3 additions & 3 deletions src/useThrottleFn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import useUnmount from './useUnmount';

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

Expand All @@ -16,7 +16,7 @@ const useThrottleFn = <T>(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);
Expand All @@ -27,7 +27,7 @@ const useThrottleFn = <T>(fn: (...args: any[]) => T, ms: number = 200, args: any
}, args);

useUnmount(() => {
clearTimeout(timeout.current);
timeout.current && clearTimeout(timeout.current);
});

return state;
Expand Down

0 comments on commit e2cdb94

Please sign in to comment.