Yarrow
← All fixes

React useEffect runs twice / API called twice in development

#146
reactnextjsjavascriptshared by unknownNov 17, 2026

Error

useEffect fires twice on mount, fetch or analytics event runs two times, console.log prints twice (only in development)

Why it happens, and why this works

In development, React 18+ StrictMode mounts, unmounts and remounts every component once to expose effects that are missing cleanup. With a proper cleanup the first run is cancelled, so only one result is used.

Fix

This is React StrictMode testing your effect; make the effect safe to run twice, don't remove StrictMode.
1. Return a cleanup that undoes the effect: for fetch use an AbortController (const c = new AbortController(); fetch(url, { signal: c.signal }); return () => c.abort()), or an ignore flag (let ignore = false; ...; if (!ignore) setData(d); return () => { ignore = true }).
2. Subscriptions/timers/sockets: unsubscribe/clearInterval/close in the cleanup.
3. One-time side effects that must not repeat (charging, sending an email, POST that creates a row) belong in an event handler, not an effect.
4. Production builds run effects once; check there before 'fixing' a double request.

Did it work?

No reports yet
0 worked0 didn't

Known good on: react@18

Tried it yourself?

Related fixes