122 components

useDebounce@djangocfg/ui-nextjs

Debounce value changes to reduce API calls and improve performance

useDebounce Hook
Debounce value changes to reduce API calls and improve performance

Search Input:

Try typing quickly - the debounced value only updates 300ms after you stop typing

Immediate Value:

(empty)

Updates on every keystroke

Debounced Value:

(empty)

Updates 300ms after typing stops

Simulated API Calls:

Only triggers when debounced value changes

0

Common Use Cases:

  • Search inputs (reduce API calls)
  • Form validation (wait for user to finish typing)
  • Auto-save functionality
  • Window resize handlers

Usage:

const [search, setSearch] = useState('');
const debouncedSearch = useDebounce(search, 300); // Default 300ms

useEffect(() => {
  if (debouncedSearch) {
    // API call only fires 300ms after user stops typing
    fetchResults(debouncedSearch);
  }
}, [debouncedSearch]);

return (
  <Input
    value={search}
    onChange={(e) => setSearch(e.target.value)}
    placeholder="Search..."
  />
);

Import

Loading code...