Skip to content

How to Fix Debounced Callback in React

DodaTech Updated 2026-06-26 1 min read

The Problem

In this quick-fix, you'll learn how to implement debounced callbacks with useCallback. This matters because debounced functions recreate every render losing state. In real-world applications, this pattern appears in search inputs auto-save resize handlers.

The Wrong Way

function SearchBox() {
  const [query, setQuery] = useState("");
  const debouncedSearch = debounce((q) => fetch("/api/search?q="+q), 500);
  const handleChange = (e) => { debouncedSearch(e.target.value); setQuery(e.target.value); };
  return <input onChange={handleChange} />;
}
debouncedSearch re-created every render debounce resets

The Right Way

function SearchBox() {
  const [query, setQuery] = useState("");
  const debouncedSearch = useCallback(debounce((q) => fetch("/api/search?q="+q), 500), []);
  const handleChange = (e) => { debouncedSearch(e.target.value); setQuery(e.target.value); };
  return <input onChange={handleChange} />;
}
debouncedSearch stable across renders debounce works

Prevention

  • Stabilize debounced functions with useCallback
  • Create debounced function outside component or use ref
  • Cancel debounced calls in useEffect cleanup

FAQ

### Why does debounce not work in React??

Functional components re-create debounced function every render resetting timer. Use useCallback or ref.

Should I use lodash debounce or custom??

lodash debounce well-tested handles edge cases like leading/trailing.

How to cancel debounce on unmount??

Store debounced function in ref and call .cancel() in cleanup.

This guide is brought to you by DodaTech -- built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro