Sphere
Log in Join
☆ 16

useEffect fires twice and I'm losing my mind in React 18

Our dev setup is React 18.3 with StrictMode and Vite 5.2. I have a component that fetches a user profile in a useEffect with an empty deps array, and in dev it runs twice so I get two identical POSTs to /api/profile. I know StrictMode double-invokes effects in dev, but it is also happening in the production build after we deploy to Netlify. The cleanup aborts the fetch with AbortController but the second request still goes out. Is everyone just living with this or is there an actual fix? Code is basically: useEffect(() => { loadProfile(); }, []);

⚑ Report

4 replies

☆ 0
2 2am ·2026-10-14#1

Empty deps with StrictMode will double-fire in dev, that is expected. But prod should not. Are you sure you are not calling loadProfile somewhere else too? I once had it in the component body AND the effect. Also check your build - are you actually building with npm run build and serving dist/, or did Netlify pick up the dev server?

⚑ Report
☆ 0
G gremlin ·2026-10-19#2

Turns out loadProfile was also called in an onClick of a parent that fires on mount because of a bad conditional. Dev was double, prod was double too, same root cause. Removed the extra call and the abort controller actually does its job now. Still confused why I did not see it earlier.

⚑ Report
☆ 0
A async ·2026-10-26#3

The cleanup only aborts the in-flight request from the previous effect run. If loadProfile creates a new AbortController internally each call, the first one gets aborted fine, that part was working. Your duplicate was a separate call entirely, so no amount of cleanup was going to catch it. Glad you found it.

⚑ Report
☆ 0
D doom ·2026-10-27#4

For what it is worth, we stopped putting StrictMode double-invoke at the top of our debugging list after getting burned like this twice. Check the obvious duplicate call first. If you do want the fetch to truly dedupe, use a ref guard: const ran = useRef(false); if (ran.current) return; ran.current = true;

⚑ Report
Log in to reply.