UNPKG

657 BJavaScriptView Raw
1import { useCallback } from 'react';
2import useTimeout from './useTimeout';
3/**
4 * Creates a debounced function that will invoke the input function after the
5 * specified delay.
6 *
7 * @param fn a function that will be debounced
8 * @param delay The milliseconds delay before invoking the function
9 */
10
11export default function useDebouncedCallback(fn, delay) {
12 var timeout = useTimeout();
13 return useCallback(function () {
14 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
15 args[_key] = arguments[_key];
16 }
17
18 timeout.set(function () {
19 fn.apply(void 0, args);
20 }, delay);
21 }, [fn, delay]);
22}
\No newline at end of file