mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-25 16:56:22 +02:00
24 lines
600 B
TypeScript
24 lines
600 B
TypeScript
|
|
import { useEffect, useState } from "react";
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Hook that returns a debounced value that only updates after the specified delay
|
||
|
|
* @param value - The value to debounce
|
||
|
|
* @param delay - The delay in milliseconds (default: 300ms)
|
||
|
|
* @returns The debounced value
|
||
|
|
*/
|
||
|
|
export function useDebouncedValue<T>(value: T, delay: number = 300): T {
|
||
|
|
const [debouncedValue, setDebouncedValue] = useState<T>(value);
|
||
|
|
|
||
|
|
useEffect(() => {
|
||
|
|
const timer = setTimeout(() => {
|
||
|
|
setDebouncedValue(value);
|
||
|
|
}, delay);
|
||
|
|
|
||
|
|
return () => {
|
||
|
|
clearTimeout(timer);
|
||
|
|
};
|
||
|
|
}, [value, delay]);
|
||
|
|
|
||
|
|
return debouncedValue;
|
||
|
|
}
|