mirror of
https://github.com/portainer/portainer.git
synced 2025-07-19 21:39:40 +02:00
move debouncing to the component (from the validation). debounce returns undefined when it's not calling the debounced function, and undefined is considered a validation error.
15 lines
371 B
TypeScript
15 lines
371 B
TypeScript
import { useEffect, useState } from 'react';
|
|
|
|
export function useDebouncedValue<T>(value: T, delay = 500): T {
|
|
const [debouncedValue, setDebouncedValue] = useState<T>(value);
|
|
|
|
useEffect(() => {
|
|
const timer = setTimeout(() => setDebouncedValue(value), delay);
|
|
|
|
return () => {
|
|
clearTimeout(timer);
|
|
};
|
|
}, [value, delay]);
|
|
|
|
return debouncedValue;
|
|
}
|