import { useEffect, useState } from 'react'; /** * Debounce a value with a specified delay * Useful for search inputs, filters, and other frequently changing values * * @param value - The value to debounce * @param delay - Delay in milliseconds (default: 300ms) * @returns The debounced value * * @example * ```tsx * function SearchInput() { * const [searchTerm, setSearchTerm] = useState(''); * const debouncedSearchTerm = useDebounce(searchTerm, 500); * * useEffect(() => { * // This will only run when user stops typing for 500ms * if (debouncedSearchTerm) { * performSearch(debouncedSearchTerm); * } * }, [debouncedSearchTerm]); * * return setSearchTerm(e.target.value)} />; * } * ``` */ export function useDebounce(value: T, delay: number = 300): T { const [debouncedValue, setDebouncedValue] = useState(value); useEffect(() => { // Set up the timeout to update debounced value after delay const timer = setTimeout(() => { setDebouncedValue(value); }, delay); // Clean up the timeout if value changes or component unmounts return () => { clearTimeout(timer); }; }, [value, delay]); return debouncedValue; }