π Hey folks! I’m Rajat Sharma, and today weβre diving into an underrated but powerful concept: Debouncing.
Have you ever typed in a search bar and noticed how some apps wait a moment before responding?
Thatβs not lag β itβs debouncing, and itβs intentional.
In this article, you’ll learn:
- What debouncing is
- Why it matters in modern web apps
- How to implement it in React (with real API examples!)
- And the trade-off it brings
Letβs go! π§ β¨
π§© What is Debouncing?
Debouncing is a technique used to delay the execution of a function until a certain time has passed since it was last called.
This is super useful for:
- Typing in input fields
- Window resizing
- Rapid clicking
Without debouncing, each keystroke can fire an API call. Thatβs a lot of unnecessary traffic! π±
π§ͺ Real-Life Use Case
You’re building a postal code search.
User types 800001
β should we fire six API calls? NO!
Instead, wait a little after the last keystroke β only fire one request. β
π§° Basic Setup (Without Debounce)
import axios from "axios";
import React from "react";
export default function App() {
const setInput = (value) => {
axios.get(`https://api.postalpincode.in/pincode/${value}`)
.then((res) => {
console.log(res.data[0]?.PostOffice[0]);
});
};
return (
<div>
<input
placeholder="Enter Pincode..."
onChange={(e) => setInput(e.target.value)}
/>
div>
);
}
β Problem: Every keypress hits the API. Not efficient.
β
Debouncing with setTimeout
+ useEffect
Letβs optimize it!
1οΈβ£ Add state
const [pinCode, setPinCode] = React.useState("");
2οΈβ£ Update input on change
<input
placeholder="Enter Pincode..."
onChange={(e) => setPinCode(e.target.value)}
/>
3οΈβ£ Debounce API call in useEffect
React.useEffect(() => {
const timer = setTimeout(() => {
if (pinCode) {
axios.get(`https://api.postalpincode.in/pincode/${pinCode}`)
.then((res) => {
console.log(res.data[0]);
});
}
}, 2000); // 2 second debounce
return () => clearTimeout(timer);
}, [pinCode]);
β Now the API call triggers only after user stops typing for 2 seconds.
π§© Full Working Code
import axios from "axios";
import React from "react";
export default function App() {
const [pinCode, setPinCode] = React.useState("");
React.useEffect(() => {
const timer = setTimeout(() => {
if (pinCode) {
axios
.get(`https://api.postalpincode.in/pincode/${pinCode}`)
.then((res) => {
console.log(res.data[0]);
});
}
}, 2000);
return () => clearTimeout(timer);
}, [pinCode]);
return (
<div>
<input
placeholder="Enter Pincode..."
onChange={(e) => setPinCode(e.target.value)}
/>
div>
);
}
π Now typing 800001
triggers just one API call!
π― Where Else Can You Use Debouncing?
- π Search Bars
- π Form Auto-save
- πͺ Resize Listeners
- π±οΈ Button Clicks
π§ Bonus: Use lodash.debounce
for Cleaner Code
Install it:
npm install lodash.debounce
Use it:
import debounce from "lodash.debounce";
const debouncedFetch = debounce((value) => {
axios.get(`https://api.postalpincode.in/pincode/${value}`).then(...);
}, 2000);
This makes your code reusable and cleaner!
βοΈ Is Debouncing Making Your App Feel Slow?
That’s a good observation! Here’s the deal:
β
Pros:
- Prevents API spamming
- Smooths out performance
- Saves server resources
β οΈ Cons:
- Adds a perceived delay to the response
- Not ideal when users expect instant feedback
π§ My Take:
If your use case involves:
- Heavy API calls
- User typing/searching
β Use shorter debounce delays (e.g., 300-500ms)
Use 2000ms
only when response is non-urgent or API is expensive.
π§ Final Thoughts
Debouncing is a must-know for every developer.
It helps create apps that are efficient, responsive, and smooth.
So next time your UI lags behind your API β debounce it, donβt spam it!
π Stay Connected
If this helped you out, drop a π¬ or β€οΈ below!
π§βπ» Happy Coding!
Let me know β Iβd love to help you make this article shine even more!