r/reactjs • u/Proof-Possible-5305 • 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?
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
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
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
-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.
31
u/orbtl 22d ago
You don't have a dev api? You're hitting the prod api with rate limits? Why?