redux-toolkit
redux-toolkit copied to clipboard
onQueryStarted RTK Query triggers twice
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
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.
@santirodriguezaffonso You may duplicate a middleware in your store. It happened with me as well.
Closing due to lack of repro.
@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 🤝🏻
There should be an error message for duplicate middlewares because I encountered this and I've been battling with it for days.
@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?
Well, I have a base API where I inject endpoints. I managed to call it separately again by mistake
@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].
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.
@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
baseApiSlicewhere we inject every newapiSlicefile. So obviously[baseApiSlice.middleware]is passed to thestore. The problem appeared when a dev mistakenly added a new middleware from anotherapiSlicealready 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.