UNPKG

@geneui/components

Version:

The Gene UI components library designed for BI tools

29 lines (26 loc) 700 B
import { useState, useEffect } from 'react'; const useDebounce = (value, delay) => { if (value === undefined) { let timeoutId; const debounceCallback = (value, delay) => { clearTimeout(timeoutId); timeoutId = setTimeout(value, delay); }; const clearDebounce = () => clearTimeout(timeoutId); return { debounceCallback, clearDebounce }; } const [debouncedValue, setDebouncedValue] = useState(value); useEffect(() => { const handler = setTimeout(() => { setDebouncedValue(value); }, delay); return () => { clearTimeout(handler); }; }, [delay, value]); return debouncedValue; }; export { useDebounce as default };