useDebounce — Debounced Value Hook
Debounce any rapidly-changing value with a configurable delay. Useful for search inputs and resize handlers.
import { useState, useEffect } from 'react';
export function useDebounce<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;
}
// Usage:
// const [query, setQuery] = useState('');
// const debouncedQuery = useDebounce(query, 300);
//
// useEffect(() => {
// if (debouncedQuery) fetchResults(debouncedQuery);
// }, [debouncedQuery]);Use Cases
- Search-as-you-type
- Window resize handlers
- Form auto-save
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
Infinite Scroll with Intersection Observer
Load more items as the user scrolls using IntersectionObserver. No external libraries required.
useFetch — Generic Data Fetching Hook
Custom React hook for data fetching with loading, error, and refetch states using the Fetch API.
useLocalStorage — Persistent State Hook
Sync React state with localStorage including SSR safety, JSON serialization, and cross-tab updates.
useClickOutside — Outside Click Detection
Detect clicks outside a referenced element to close dropdowns, modals, and popover menus.