redux-toolkit icon indicating copy to clipboard operation
redux-toolkit copied to clipboard

onQueryStarted RTK Query triggers twice

Open santirodriguezaffonso opened this issue 1 year ago • 1 comments

As the guide mentions, I went first to StackOverflow so for Context and Code go to: https://stackoverflow.com/questions/78329782/onquerystarted-rtk-query-triggers-twice

Well basically as the title mentions. I'm trying to do an optimistic update to a counter but since the onQueryStarted is getting triggered twice I end up with a wrong value.

Alternatively, since I'm in the development stage I've been using the Tags but is not the best performant option for Prod.

Goal

Update the user counting by reducing it by one 'cause I'm removing only one user.

Actual Result

So in the case, I have 10 users result should be 9 but I'm getting 8 as result of onQueryStarted 'cause it's getting called twice

santirodriguezaffonso avatar Apr 15 '24 17:04 santirodriguezaffonso

Could you please create an isolated reproduction for that? I've never seen this behaviour anywhere and fear we won't be able to reproduce it.

phryneas avatar Apr 15 '24 21:04 phryneas

@santirodriguezaffonso You may duplicate a middleware in your store. It happened with me as well.

VladimirPal avatar Jul 22 '24 08:07 VladimirPal

Closing due to lack of repro.

markerikson avatar Aug 30 '24 19:08 markerikson

@santirodriguezaffonso You may duplicate a middleware in your store. It happened with me as well.

This was the cause! Thank you for pointing it out dude 🤝🏻

santirodriguezaffonso avatar Mar 31 '25 01:03 santirodriguezaffonso

There should be an error message for duplicate middlewares because I encountered this and I've been battling with it for days.

Rufai-Ahmed avatar Apr 06 '25 11:04 Rufai-Ahmed

@Rufai-Ahmed I'm not immediately sure if there's a way for us to detect that a given middleware has been added twice. Beyond that, this should be a pretty rare error. How did you manage to add the same middleware twice?

markerikson avatar Apr 06 '25 16:04 markerikson

Well, I have a base API where I inject endpoints. I managed to call it separately again by mistake

Rufai-Ahmed avatar Apr 06 '25 16:04 Rufai-Ahmed

@Rufai-Ahmed I'm not immediately sure if there's a way for us to detect that a given middleware has been added twice. Beyond that, this should be a pretty rare error. How did you manage to add the same middleware twice?

In my case, there's a baseApiSlice where we inject every new apiSlice file. So obviously [baseApiSlice.middleware] is passed to the store. The problem appeared when a dev mistakenly added a new middleware from another apiSlice already injected into the base one. Ending up with:[baseApiSlice.middleware, messagesApiSlice.middleware].

santirodriguezaffonso avatar Apr 07 '25 01:04 santirodriguezaffonso

Went ahead and added a new dev-mode check to configureStore that will look for duplicate middleware references and throw an error. That'll be out in 2.7.0 in the near future.

markerikson avatar Apr 07 '25 02:04 markerikson

@Rufai-Ahmed I'm not immediately sure if there's a way for us to detect that a given middleware has been added twice. Beyond that, this should be a pretty rare error. How did you manage to add the same middleware twice?

In my case, there's a baseApiSlice where we inject every new apiSlice file. So obviously [baseApiSlice.middleware] is passed to the store. The problem appeared when a dev mistakenly added a new middleware from another apiSlice already injected into the base one. Ending up with:[baseApiSlice.middleware, messagesApiSlice.middleware].

Exactly this. I wasn't descriptive enough. And thanks @markerikson for looking into it.

Rufai-Ahmed avatar Apr 07 '25 06:04 Rufai-Ahmed