grungnet/src/hooks/useDebounce.ts

19 lines
426 B
TypeScript
Raw Normal View History

2020-04-23 03:23:34 -04:00
import { useEffect, useState } from "react";
2021-07-17 19:12:32 -04:00
function useDebounce<Type>(value: Type, delay: number): Type | undefined {
const [debouncedValue, setDebouncedValue] = useState<Type>();
2020-04-23 03:23:34 -04:00
useEffect(() => {
const timeout = setTimeout(() => {
setDebouncedValue(value);
}, delay);
return () => {
clearTimeout(timeout);
};
}, [value, delay]);
return debouncedValue;
}
export default useDebounce;