mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-25 16:56:22 +02:00
18 lines
454 B
TypeScript
18 lines
454 B
TypeScript
import * as React from "react";
|
|
|
|
export const useDebounce = <T>(value: T, delay = 500) => {
|
|
const [debouncedValue, setDebouncedValue] = React.useState(value);
|
|
|
|
React.useEffect(() => {
|
|
const handler: NodeJS.Timeout = setTimeout(() => {
|
|
setDebouncedValue(value);
|
|
}, delay);
|
|
|
|
// Cancel the timeout if value changes (also on delay change or unmount)
|
|
return () => {
|
|
clearTimeout(handler);
|
|
};
|
|
}, [value, delay]);
|
|
|
|
return debouncedValue;
|
|
};
|