Files
sign/packages/lib/client-only/hooks/use-debounced-value.ts

19 lines
429 B
TypeScript
Raw Normal View History

2023-08-07 23:10:27 +10:00
import { useEffect, useState } from 'react';
export function useDebouncedValue<T>(value: T, delay: number) {
// State and setters for debounced value
const [debouncedValue, setDebouncedValue] = useState(value);
useEffect(() => {
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delay);
return () => {
clearTimeout(handler);
};
}, [value, delay]);
return debouncedValue;
}