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 32 33 34 35 36 37 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 5x 2x 5x 5x 5x 7x 7x 7x 7x | /**
* @file useDebounced.ts
* @description
* Generic debounce hook for delaying value updates.
* Useful for debouncing search queries, text input, or any value type.
* Reduces server chatter and UI updates while user is typing.
*
* @usage
* const debouncedSearchTerm = useDebounced(searchTerm, 350)
* const debouncedQuery = useDebounced(itemQuery, 250)
*/
import * as React from 'react';
/**
* Debounce a value by a specified delay in milliseconds.
* @template T - The type of value to debounce
* @param value - The value to debounce
* @param delayMs - Delay in milliseconds (default: 250)
* @returns The debounced value
* @example
* const debouncedSearch = useDebounced(searchInput, 500)
*/
export function useDebounced<T>(value: T, delayMs: number = 250): T {
const [debouncedValue, setDebouncedValue] = React.useState(value);
// Update debounced value after delay
React.useEffect(() => {
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delayMs);
// Cleanup timeout if value or delay changes
return () => clearTimeout(handler);
}, [value, delayMs]);
return debouncedValue;
}
|