react-use/src/useDebounce.ts
2019-07-17 00:21:06 +02:00

15 lines
349 B
TypeScript

import useUpdateEffect from './useUpdateEffect';
const useDebounce = (fn: () => any, ms: number = 0, args: any[] = []) => {
useUpdateEffect(() => {
const handle = setTimeout(fn.bind(null, args), ms);
return () => {
// if args change then clear timeout
clearTimeout(handle);
};
}, args);
};
export default useDebounce;