import { useState, useEffect } from 'react'; /** * Hook that debounces a value by the specified delay. * * @param value - The value to debounce * @param delay - Delay in milliseconds (default: 300) * @returns The debounced value * * @example * ```tsx * const [searchTerm, setSearchTerm] = useState(''); * const debouncedSearch = useDebounce(searchTerm, 500); * * // debouncedSearch updates 500ms after searchTerm stops changing * useEffect(() => { * if (debouncedSearch) { * performSearch(debouncedSearch); * } * }, [debouncedSearch]); * ``` */ export function useDebounce(value: T, delay: number = 300): T { const [debouncedValue, setDebouncedValue] = useState(value); useEffect(() => { const timer = setTimeout(() => { setDebouncedValue(value); }, delay); return () => { clearTimeout(timer); }; }, [value, delay]); return debouncedValue; } export default useDebounce;