Add "useDebounce" hook

This commit is contained in:
Justin 2022-08-09 14:19:19 -04:00
parent 27df2b617e
commit 2c6c281568
2 changed files with 18 additions and 0 deletions

View file

@ -2,6 +2,7 @@ export { useAccount } from './useAccount';
export { useApi } from './useApi';
export { useAppDispatch } from './useAppDispatch';
export { useAppSelector } from './useAppSelector';
export { useDebounce } from './useDebounce';
export { useDimensions } from './useDimensions';
export { useFeatures } from './useFeatures';
export { useLocale } from './useLocale';

View file

@ -0,0 +1,17 @@
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 };