{"success":true,"name":"write-react-effects-that-survive-running-twice-in-developmen","description":"Working in React and an effect fires twice, duplicating requests or subscriptions","category":"ai-brimind","path":"ai-brimind/write-react-effects-that-survive-running-twice-in-developmen/SKILL.md","file":"SKILL.md","content":"---\nname: write-react-effects-that-survive-running-twice-in-developmen\ndescription: \"Working in React and an effect fires twice, duplicating requests or subscriptions\"\n---\n\n# Write React Effects That Survive Running Twice in Development\n\n## When to use\nProject is React 18+ with StrictMode, and a useEffect performs fetches, subscriptions, timers, or logging that misbehaves when mounted, unmounted, and mounted again in dev.\n\n## Example request\nfetch in useEffect fires twice on load in dev → StrictMode mounts, unmounts, remounts on purpose; not a bug, a test. subscription added twice → the effect has no cleanup returning the unsubscribe; failed the rehearsal. removing StrictMode to 'fix' it → the detector was unplugged, the leak remains; muted alarm. — each effect wasn't written to be repeatable.\n\n## Instructions\nAct as a React mechanic who knows the dev double-run is a stress test, not a malfunction. React's StrictMode deliberately mounts, unmounts, and remounts components in development to expose effects that can't handle their own lifecycle — so an effect firing twice is React asking: 'do you clean up properly, and are you safe to repeat?' Never answer by removing StrictMode; answer by fixing the effect. Every effect that starts something must return a cleanup that fully undoes it: unsubscribe, clear the timer, abort the in-flight fetch (wire an abort controller and cancel it in cleanup), so the pair mount+unmount leaves no trace and the second mount starts clean. For work that genuinely must happen once per user action — not per mount — move it out of effects entirely, into the event handler that caused it. Verify by intent: with StrictMode on, the app must behave identically to production, just rehearsed twice. Only proceed once every effect's setup and cleanup form a true pair — the double-run then becomes free proof instead of noise.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #743\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}