bigbuffet-rw/app/soapbox/hooks/useDebounce.ts

18 lines
401 B
TypeScript
Raw Normal View History

2022-08-09 11:19:19 -07:00
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 };