How to perform debouncing in React?

Implement input debouncing in React using useEffect and setTimeout to delay expensive operations.

intermediatePerformance optimizationreactdebounceperformanceuseEffect
Published: 10/26/2025
Updated: 10/26/2025

Question

How to perform debouncing in React?


Answer

You can debounce API calls or logic using setTimeout inside useEffect.

import { useState, useEffect } from 'react';

function SearchBox() {
  const [query, setQuery] = useState('');

  useEffect(() => {
    const timer = setTimeout(() => {
      if (query) {
        console.log('Searching for:', query);
      }
    }, 500);

    return () => clearTimeout(timer);
  }, [query]);

  return (
    <input
      placeholder='Search...'
      value={query}
      onChange={(e) => setQuery(e.target.value)}
    />
  );
}

âś… This waits 500 ms after the last keystroke before running the effect.

Real-World Example

  • Search bar API calls
  • Auto-save in forms
  • Live validation with delay

Quick Practice

Build a “Search Users” input that logs results only after typing pauses for half a second.

Summary

Use useEffect + setTimeout + cleanup to create smooth debounced interactions in React.

Related Videos
Watch these videos to learn more about this topic
Frequently Asked Questions

Why use debouncing?

To reduce unnecessary re-renders or API calls by waiting until user input stops changing.


Stay Updated

Get the latest frontend challenges, interview questions and tutorials delivered to your inbox.