MCPcopy Create free account
hub / github.com/atomicdata-dev/atomic-data-browser / useDebounce

Function useDebounce

react/src/useDebounce.ts:4–26  ·  view source on GitHub ↗
(value: T, delay: number)

Source from the content-addressed store, hash-verified

2
3// T is a generic type for value parameter, our case this will be string
4export function useDebounce<T>(value: T, delay: number): T {
5 // State and setters for debounced value
6 const [debouncedValue, setDebouncedValue] = useState<T>(value);
7
8 useEffect(
9 () => {
10 // Update debounced value after delay
11 const handler = setTimeout(() => {
12 setDebouncedValue(value);
13 }, delay);
14
15 // Cancel the timeout if value changes (also on delay change or unmount)
16 // This is how we prevent debounced value from updating if value is changed ...
17 // .. within the delay period. Timeout gets cleared and restarted.
18 return () => {
19 clearTimeout(handler);
20 };
21 },
22 [value, delay], // Only re-call effect if value or delay changes
23 );
24
25 return debouncedValue;
26}

Callers 3

useValueFunction · 0.90
useServerSearchFunction · 0.90
useLocalSearchFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected