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(); }, []);
4 replies