react-use/src/useDebounce.ts
2019-04-12 23:18:11 +10:00

15 lines
329 B
TypeScript

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