Add "useDebounce" hook
This commit is contained in:
parent
27df2b617e
commit
2c6c281568
2 changed files with 18 additions and 0 deletions
|
@ -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';
|
||||
|
|
17
app/soapbox/hooks/useDebounce.ts
Normal file
17
app/soapbox/hooks/useDebounce.ts
Normal 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 };
|
Loading…
Reference in a new issue