StackDevLife
1-Min FixBeginnerJavaScript / React / Next.js / Angular

Add loading guard to stop duplicate API calls

~1 min read
TL;DR

One missing loading check can create duplicate records, double payments, or repeated requests.

A very common real-world issue:
User clicks button twice.
API fires twice.
Now you have duplicate inserts, duplicate emails, or duplicate submissions.

Quick frontend fix:

JavaScript
if (loading) return;
setLoading(true);

try {
  await submitData();
} finally {
  setLoading(false);
}

Also disable the button:

JavaScript
<button disabled={loading}>
  {loading ? "Submitting..." : "Submit"}
</button>

This is small, but in real apps it prevents messy data problems.

APIfrontendduplicate requestloadingUX

Get a new 1-Min Fix in your inbox

Subscribe to Stack Dev Life — free, no spam, unsubscribe anytime.

Subscribe free →