ghost-portal/packages/react-hooks/src/debounce.tsx
Jaco Greeff 03158fb594
Add useFavorites hook (incl. react-hooks package) (#1873)
* Add useFavorites hook

* useFavorites to Nominate

* Fix filter: grayscale positioning
2019-11-12 18:32:52 +01:00

24 lines
664 B
TypeScript

// Copyright 2017-2019 @polkadot/react-components 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;
}