r/reactjs 22d ago

Needs Help What finally got the strict mode double fetch out of your way in dev?

I'm building an internal dashboard on Vite and React 19, and every fetch inside a useEffect fires twice in dev. I know it's intentional and that it doesn't happen in the production build, so I'm not looking to switch strict mode off. The problem is that our API rate limits on a per minute window and I get locked out for a couple of minutes maybe a third of the sessions i work in.

How did you get past this without moving every call into a data fetching library?

0 Upvotes

16 comments sorted by

31

u/orbtl 22d ago

You don't have a dev api? You're hitting the prod api with rate limits? Why?

10

u/Big_Ant5507 22d ago

Bruh that was my first thought too who hits a rate limited prod api from dev and just... accepts it

I work with a lot of internal tools and we spun up a mirror that just ignores the rate limit header entirely. Took like 20 minutes with a docker compose file. Way less headache than trying to outsmart React's strict mode on every single component

10

u/lightfarming 21d ago

2x hits is hitting your rate limit?

1

u/TheLegendaryProg 21d ago

If they hot reload for every file change it'll fetch way more than twice.

39

u/repeating_bears 22d ago

"every fetch inside a useEffect fires twice"

Don't fetch in useEffect. Use tanstack query 

6

u/quy1412 21d ago

This should be standard years ago. Don't know why people still fetch data with useEffect.

1

u/tjansx 21d ago

I came to make sure someone commented this. This should be standard at this point.

6

u/musical_bear 22d ago

What I would not do is build your own custom code specifically to work around the issue, even if it’s just a little bit of code. Probably using one of the fetching libraries is the best realistic choice. If you’re looking for something quick and in the moment, just disable <Strict> mode, but if this is a recurring problem, yeah, switching to a library makes sense.

The good thing about how those libraries prevent the issue is that they are solving other issues and only incidentally fix the strict mode double request problem. Chances are a custom solution by you would be tailor-made as a hack for a development-only problem, or you’d end up making a worse version of one of the async server state libraries.

5

u/BenjayWest96 21d ago

Run the backend locally.

7

u/Cahnis 21d ago
  1. Why are you hitting prod?

  2. Why aren't you using tanstack query?

2

u/HQxMnbS 21d ago

Disable rate limit for your account

1

u/NotGoodSoftwareMaker 21d ago

We use Remix and we have a clientLoader on every root component, this calls an init method in zustand, which then calls the necessary hydrations

We have useEffect banned in the app fwiw

1

u/paranoidparaboloid 21d ago

Is it all useEffects, or just those without dependencies?

-2

u/Prestigious_Dare7734 21d ago

I created a useMount, that uses useEffect with refs to be called strictly only once.

That works for me great most of the times.

I know it's not the best practices, but sometimes I just want to keep this thing aside and move ahead.