18 lines
401 B
TypeScript
18 lines
401 B
TypeScript
|
import { useEffect, useState } from 'react';
|
||
|
|
||
|
const useDebounce = (value: string, delay: number): string => {
|
||
|
const [debouncedValue, setDebouncedValue] = useState<string>(value);
|
||
|
|
||
|
useEffect(() => {
|
||
|
const timer = setTimeout(() => setDebouncedValue(value), delay);
|
||
|
|
||
|
return () => {
|
||
|
clearTimeout(timer);
|
||
|
};
|
||
|
}, [value, delay]);
|
||
|
|
||
|
return debouncedValue;
|
||
|
};
|
||
|
|
||
|
export { useDebounce };
|