ghost-portal/packages/react-hooks/src/useDebounce.tsx
Jaco Greeff c167227845
Add useApiContext (#1890)
* Add useApiContext

* Add missing withApiContext import

* useCacheKey, rename headers

* Adjustment on sessionRewards naming

* Renames based on defacto standards
2019-11-15 10:51:12 +01:00

24 lines
659 B
TypeScript

// Copyright 2017-2019 @polkadot/react-hooks authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { useState, useEffect } from 'react';
// Debounces inputs
export default function useDebounce <T> (value: T, delay = 250): T {
const [debouncedValue, setDebouncedValue] = useState(value);
useEffect((): () => void => {
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delay);
// each time it renders, it clears
return (): void => {
clearTimeout(handler);
};
}, [value]);
return debouncedValue;
}