Why does useEffect run two times? How to fix the useEffect runs twice problem

Опубликовано: 28 Июнь 2023
на канале: Code With Yousaf
3,617
65

How to fix the useEffect runs twice problem
React 18 released in March 2022 changed the default behavior of useEffect().
I didn’t even realize it at first, reading the React 18 launch post, buried along a lot of new features.
If your application is behaving strangely after updating to React 18, the default behavior of useEffect changed to run it 2 times.
Just in development mode, but this is the mode everyone builds their application on.
And just in strict mode, but that’s now the default for applications built using Vite, create-react-app or Next.js.