Join waitlist
ReactCommunity question

useEffect running twice?

Asked by Tushar Goyal 28 days ago 2 answers 6 votes

useEffect with [] calls my API twice. only in dev, production build is fine. what am I doing wrong

2 answers

Tanvi Kulkarni28 days agoMost voted6

Nothing wrong, that's StrictMode in development. React mounts, unmounts and mounts again to check your effects clean up properly. Add a cleanup (AbortController for fetch) and it's fine. Doesn't happen in production.

Tushar Goyal28 days ago

ah ok. thanks

Related questions

Go deeper

Want the full explanation, not just an answer?

Structured lessons on DSA, core CS, Java and interview skills, each ending in a quiz so you know it stuck.