All files / benchmark/src useDebouncedValue.ts

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

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      39x   39x 20x 2x     20x 20x       39x    
import { useEffect, useState } from "react";
 
export function useDebouncedValue<T>(value: T, delayMs = 180): T {
  const [debouncedValue, setDebouncedValue] = useState(value);
 
  useEffect(() => {
    const timeoutId = window.setTimeout(() => {
      setDebouncedValue(value);
    }, delayMs);
 
    return () => {
      window.clearTimeout(timeoutId);
    };
  }, [delayMs, value]);
 
  return debouncedValue;
}