Full answer
Typing "react" quickly fires five change events. Sending a request for each one is wasteful and can show out-of-order results. Debouncing waits until the input is quiet for a short time.
The hook is small. It holds debounced in state, initialised with the current value. An effect depends on value and delay. It starts setTimeout(() => setDebounced(value), delay) and returns () => clearTimeout(timer). When the value changes before the timer fires, React runs the cleanup first, which cancels the pending update, then the effect starts a fresh timer. After the user pauses, the timer fires once.
The component keeps two values: the raw input value for the text box, so typing feels instant, and the debounced value for the expensive work. A second effect, or a query keyed on the debounced value, performs the search. Because it depends only on the debounced value, it runs once per pause.
Choosing the delay: 200 to 400 milliseconds feels right for search. Do not debounce the input's own state, or the text box will lag.
Related ideas. Throttling limits how often something runs during a continuous event such as scroll, instead of waiting for it to stop. useDeferredValue is React's built-in way to lower the priority of rendering, but it does not delay network requests. A debounced search should also cancel in-flight requests when a new one starts.
The example logs each search to show it runs once per pause.
import { useEffect, useState } from 'react';
function useDebounce(value, delay) {
const [debounced, setDebounced] = useState(value);
useEffect(() => {
const timer = setTimeout(() => setDebounced(value), delay);
return () => clearTimeout(timer);
}, [value, delay]);
return debounced;
}
export default function Search() {
const [text, setText] = useState('');
const query = useDebounce(text, 150);
useEffect(() => {
if (query !== '') console.log('searching for ' + query);
}, [query]);
return (
<div>
<input value={text} onChange={(e) => setText(e.target.value)} placeholder="Search" />
<p>
Typed: {text} | Searching: {query}
</p>
</div>
);
}The check typed a, ab and abc in quick succession, then waited. The search ran once, for the final value. Output of the run: right after typing: Typed: abc | Searching: searching for abc after the pause: Typed: abc | Searching: abc
Key points
- Keep raw input state for the field and a debounced copy for expensive work.
- The effect sets a timer and its cleanup clears it, so only the last value wins.
- Typical delay for search is 200 to 400 milliseconds.
- Debounce waits for quiet. Throttle limits frequency during a continuous event.
- Cancel stale requests as well.
Common mistakes
- Debouncing the input's own state so the text box lags.
- Forgetting to clear the timer in the cleanup.
- Leaving delay out of the dependency array.
- Debouncing but not cancelling the previous request.
- Confusing debounce with throttle.
Follow-ups
- How would you write a useThrottle hook?
- How is useDeferredValue different from debouncing?
- How would you cancel a fetch started by an earlier search?
Tips
- Explain the two values (raw and debounced) first.
- Walk through what the cleanup does on each keystroke.
- Contrast with throttle to show vocabulary.