React 18 StrictMode runs effects twice: the fetch abort fix
Somebody on the team opened a ticket that said double API calls on page load. I assumed it was a bad dependency in a useEffect and went looking for the usual suspect: an object literal in the dependency array.
It was StrictMode. We were on React 18.3.1 with Vite 5.2 and had wrapped the whole app in <StrictMode> about two months earlier as part of a cleanup PR. Nobody noticed until a new endpoint started writing a row on every GET.
## What StrictMode is actually doing
In development, StrictMode mounts a component, runs its effects, cleans them up, and mounts it again. It does this deliberately to surface effects that cannot be safely cleaned up. In production the double invocation does not happen.
This is not a bug in React and it is not specific to fetching. It catches subscriptions that are never removed, intervals that keep running, and event listeners that pile up. But the thing that makes it painful is that an unconsumed cleanup often looks completely fine until the second mount.
## The error in the console
Our first attempt was to add an AbortController and call abort() in the cleanup. That is the right idea and I still recommend it, but we wrote it wrong:
Uncaught (in promise) DOMException: The user aborted a request.
at fetch (index.js:42)
The abort fired while the first request was in flight, the rejection went unhandled, and React's error overlay lit up. In the browser console you also get a red Failed to load resource: net::ERR_ABORTED line which looks alarming and is not, by itself, a problem.
The reason it was unhandled is that we were doing this:
js
useEffect(() => {
const controller = new AbortController();
fetch('/api/orders', { signal: controller.signal })
.then(r => r.json())
.then(setOrders);
return () => controller.abort();
}, []);
When the signal aborts, fetch rejects. There is no catch, so the rejection escapes. Under StrictMode in dev, the cleanup runs immediately, so it happens every single time.
## The pattern we landed on
Two changes. First, catch the abort explicitly and ignore only that error:
js
useEffect(() => {
const controller = new AbortController(); async function load() {
try {
const res = await fetch('/api/orders', { signal: controller.signal });
if (!res.ok) throw new Error(orders: ${res.status});
setOrders(await res.json());
} catch (err) {
if (err.name !== 'AbortError') {
setError(err.message);
}
}
}
load();
return () => controller.abort();
}, []);
Second, and this is the part that actually solved the ticket, we moved the POST out of the effect. Creating a row on mount is a side effect that should be triggered by a user action. Once that write became a button handler, the duplicate row problem went away whether or not StrictMode was on.
If you cannot change the endpoint, there is a useRef guard that suppresses the second run in development, but I would treat that as a last resort. It hides the warning without fixing the underlying lifecycle problem.
## One more thing
React 19 changes some of this, but as of 18.3 the double-invoke is here to stay in dev. Turn it off only if you are doing a one-off debugging session. I left it on and fixed four other cleanup bugs in the same component within an hour, which was the point all along.