Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 287x 287x 213x 14x 213x 213x 287x | /**
* useDebounce Hook
*
* Debounces a value by delaying its update until after a specified delay.
* Useful for auto-save, search input, and API call optimization.
*
* @param value - The value to debounce
* @param delay - Delay in milliseconds (default: 500ms)
* @returns The debounced value
*/
import { useEffect, useState } from 'react';
export function useDebounce<T>(value: T, delay: number = 500): T {
const [debouncedValue, setDebouncedValue] = useState<T>(value);
useEffect(() => {
// Set up the timeout
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delay);
// Clean up the timeout if value changes before delay
return () => {
clearTimeout(handler);
};
}, [value, delay]);
return debouncedValue;
}
|