| 1 |
import { useRef, useCallback } from 'react'; |
| 2 |
|
| 3 |
export default function useDebouncedCallback( callback, wait ) { |
| 4 |
const timeout = useRef(); |
| 5 |
|
| 6 |
return useCallback( |
| 7 |
( ...args ) => { |
| 8 |
const later = () => { |
| 9 |
clearTimeout( timeout.current ); |
| 10 |
|
| 11 |
callback( ...args ); |
| 12 |
}; |
| 13 |
|
| 14 |
clearTimeout( timeout.current ); |
| 15 |
|
| 16 |
timeout.current = setTimeout( later, wait ); |
| 17 |
}, |
| 18 |
[ callback, wait ], |
| 19 |
); |
| 20 |
} |
| 21 |
|