useDebounce Debounce Hook

Last updated: 2026-11-02

useDebounce Debounce Hook

useDebounce takes a value that changes rapidly, such as the text of a search box, and returns a stable version of it only after N milliseconds: if the value changes again in the meantime, the timer restarts. Driving your requests with it removes a large amount of pointless work, which makes it a staple for search, autocomplete and resize handling. The value it returns lags behind by design, and that lag is exactly what filters out the intermediate keystrokes.

GroupComponent Patterns
KindHook
SinceReact 16.8

ℹ️ Info The editor transpiles JSX to JavaScript and renders the real UI in a sandbox; components mount automatically — no manual render call needed.

📝 Syntax

function useDebounce(value, delay = 300) {
  const [debounced, setDebounced] = useState(value)
  useEffect(() => {
    const t = setTimeout(() => setDebounced(value), delay)
    return () => clearTimeout(t)   // the value changed again: drop the old timer
  }, [value, delay])
  return debounced
}

⚙️ Parameters

valueThe raw value to debounce, usually the state of a controlled input.
delayThe debounce delay in milliseconds, 300 by default. The new value is only returned after the value has been quiet for that long.

Returns:Returns the delayed, stable value: it stays unchanged while the raw value keeps changing and only catches up after a pause.

▶ Example

The example runs out of the box: it auto-renders when expanded, and edits take effect instantly.

Output:

While you type quickly the "Debounced" line stands still and only catches up half a second after you stop — a request driven by it fires once, with the final value.

💡 Key Points

💡 Tip Use the debounced value as the dependency of a useEffect and the request naturally fires once, after the value has settled.
ℹ️ Info Debouncing waits for quiet, throttling keeps a rhythm: use debounce for search suggestions and throttle for infinite scrolling.
⚠️ Warning The effect must clear the timeout, otherwise the old timer keeps running after the component unmounts or the value changes again.
More case studies coming soon.

📚 Related Tutorials

❓ FAQ

Q What is the difference between debounce and throttle?
A Debounce runs once after the changes stop; throttle runs at a fixed interval. Search input wants debounce, scrolling and dragging want throttle.
Q How long should the delay be?
A 300 to 500 milliseconds is the common range for search; go lower when the response must feel instant and higher when the server cost is high, weighing it against how the API behaves.
Q Why must the effect return a cleanup function?
A The effect re-runs on every change of the value; without clearing the old setTimeout the stale value would still be written and the debounce would be broken.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏