I struggled with this React concept more than I expected… 👇
👉 Why are my API calls running twice?
⚙️ What you’ll notice
You open your network tab and suddenly see this:
api/me → called twice
api/roles → called twice
api/permissions → called twice
Just like in the screenshot 👇
Same request, duplicated… again and again.
⚙️ What’s actually happening
In React (development mode), if your app is wrapped in Strict Mode, React will run effects twice on purpose.
useEffect(() => {
fetch("/api/users")
.then(res => res.json())
.then(setUsers);
}, []);
Even though it looks like it should run once… it doesn’t (in dev).
🧠 What’s going on under the hood
React basically does a quick cycle:
mount → unmount → remount
Why?
To catch hidden side effects
To check if cleanup is handled properly
To make sure your logic doesn’t break on re-renders
So if your API call runs twice, React is just making sure your code can handle it.
💡 The important part
This only happens in development
Production behaves normally (runs once)
Your side effects should be safe to run multiple times
🚀 Final thought
If your network tab looks “noisy” like the screenshot,
it’s not React being broken — it’s React being careful.
And once you understand this, debugging becomes a lot less confusing.
#React #Frontend #JavaScript #WebDevelopment #ReactJS #SoftwareEngineering
Considering React Query, the old data will be removed without need of remembering to cancel before fetch.If you really need two calls to do fetch, maybe is better to put controller.abort() inner fetchData function.