All files / src/hooks useDebouncedValue.ts

100% Statements 7/7
100% Branches 1/1
100% Functions 4/4
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23                    23x   23x 17x 6x     17x     23x    
import { useState, useEffect } from "react";
 
/**
 * Hook that debounces a value by a specified delay
 *
 * @param value - The value to debounce
 * @param delay - Delay in milliseconds (default: 300ms)
 * @returns The debounced value
 */
export function useDebouncedValue<T>(value: T, delay = 300): T {
	const [debouncedValue, setDebouncedValue] = useState<T>(value);
 
	useEffect(() => {
		const timer = setTimeout(() => {
			setDebouncedValue(value);
		}, delay);
 
		return () => clearTimeout(timer);
	}, [value, delay]);
 
	return debouncedValue;
}