useEffect Fired Twice and It Found a Real Bug
useEffect fired twice, on mount, every single time, in development only. The API call inside it — a POST that created a resource — ran twice, and for about a day we had duplicate records showing up in a table that should have had exactly one insert per page load. The first reaction, and why it was wrong The instinct is to assume a bug — a rerender loop, a missing dependency, something actually…
useEffect fired twice in development mode, every single time. This issue was first attributed to a bug, but the true cause was React 18's Strict Mode. The Strict Mode deliberately mounts, unmounts, and remounts every component once to surface effects that aren't properly cleaned up. In production, this double invocation doesn't occur.
The fix involved ensuring the effect was idempotent or explicitly guarded to handle the double mount correctly. This bug was caught by Strict Mode, preventing duplicate records from appearing in the table.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.