RTK Query: dispatch inside query or mutations
See original GitHub issueI’ve tried to work with RTK Query and for now (IMHO) I think there is a decent cons of using it especially when you work with Redux (exactly what this lib is for).
In a short details:
I want to dispatch Redux actions after I get a response from query or mutation. But it’s not possible. RTKQ doesn’t provide any field to deal with dispatch. At all.
For example:
I need to query some API and then depends on a response I want to call some redux actions.
Yes, I saw in doc’s example that we can just do it in a component but imagine if I would have to do this in NNN separate components? Again and again I should write the same logic? “Hey you can do this in your custom hook” - you would say to me, but, hey, could not we do this just in a one file with the queries? That would be incredibly great if you make some feature to deal with response not in the way transformReponse does.
Especially when we use this with Redux (because loading&errors handling are great) obviously it would be great to provide some API to work with Redux actions.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:11
- Comments:16 (3 by maintainers)
Top Related StackOverflow Question
Adding to this, you generally should probably not be dispatching additional actions, but have your other slices react to the actions already being dispatched. You can add an extraReducer that just reacts to your endpoint resolving, as showcased in the authentication using extraReducers example
@magistrfox It exists in two places already depending on what you want to do, but you most likely want this: https://redux-toolkit.js.org/rtk-query/api/createApi#onquerystarted. There is an example showing
dispatchusage when a request resolves.