useDebouncedValue & useDebouncedCallback in React

From the React Custom Hooks cheat sheet · Interaction & Lifecycle Hooks · verified Jul 2026

useDebouncedValue & useDebouncedCallback

Debounce a changing value or a callback - search inputs, autosave

jsx
import { useEffect, useState, useRef, useCallback } from 'react'

function useDebouncedValue<T>(value: T, delay = 300) {
  const [debounced, setDebounced] = useState(value)
  useEffect(() => {
    const t = setTimeout(() => setDebounced(value), delay)
    return () => clearTimeout(t)
  }, [value, delay])
  return debounced
}

// Usage
function Search() {
  const [q, setQ] = useState('')
  const debouncedQ = useDebouncedValue(q, 300)
  useEffect(() => {
    if (debouncedQ) fetch('/api/search?q=' + debouncedQ)
  }, [debouncedQ])
  return <input value={q} onChange={(e) => setQ(e.target.value)} />
}
💡 useDebouncedValue for derived state; useDebouncedCallback for side effects
⚡ The fnRef pattern keeps the debounced wrapper stable as fn changes
📌 Always cancel the pending timer on unmount - stale fires can crash
🎯 Typical delays: 200-300ms for search, 500-800ms for autosave
performancedebouncesearch

Continue with React Custom Hooks

Save the full cheat sheet or work through every related task.

More React tasks

Back to the full React Custom Hooks cheat sheet