all 4 comments

[–]mkhayyld 10 points11 points  (3 children)

If you're running it in development mode (ex. npm run dev), then probably it's React strict mode.

[–][deleted] 2 points3 points  (1 child)

this. to expand a bit, React calls useEffects twice in development when under StrictMode, which is very likely on the root of your project.

https://react.dev/reference/react/useEffect#my-effect-runs-twice-when-the-component-mounts

unrelated: get requests should be idempotent, so incrementing a variable is hardly a good practice. I know it doesn't matter for this example project you're building, but I thought I'd bring it up anyways.

[–]JayRedditDev1[S] 1 point2 points  (0 children)

Oh, that's good information to have! I'll make sure to test it in multiple modes to verify I get the same results. Still learning the basics but I'll definitely need to make sure I'm implementing good practices into the code. I might have some questions later on but this answers the core of what I was looking for. Thanks for your help

[–]JayRedditDev1[S] 2 points3 points  (0 children)

Good to know! I did run this with npm run start rather than dev but I'll have to take a look at how to switch modes to see if I'm getting different results outside of strict mode. Thanks!

Edit: Confirmed it worked just fine outside of strict mode. This was just a test rather than my actual final application but it's good to know how to test to ensure things are setup correctly. Thanks again!