26 lines
703 B
TypeScript
26 lines
703 B
TypeScript
|
|
/*
|
||
|
|
* Copyright (C) Contributors to the Suwayomi project
|
||
|
|
*
|
||
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
||
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||
|
|
*/
|
||
|
|
|
||
|
|
import { useEffect, useState } from 'react';
|
||
|
|
|
||
|
|
export const useDebounce = <Value>(value: Value, delay: number): Value => {
|
||
|
|
const [debouncedValue, setDebouncedValue] = useState(value);
|
||
|
|
|
||
|
|
useEffect(() => {
|
||
|
|
const handler = setTimeout(() => {
|
||
|
|
setDebouncedValue(value);
|
||
|
|
}, delay);
|
||
|
|
|
||
|
|
return () => {
|
||
|
|
clearTimeout(handler);
|
||
|
|
};
|
||
|
|
}, [value, delay]);
|
||
|
|
|
||
|
|
return debouncedValue;
|
||
|
|
};
|