RTK query feature request: Initial Query Data and Updates from Mutation Responses #2206
Replies: 1 comment 9 replies
-
Hiya. A couple quick thoughts:
There is a draft PR for an I also haven't had time to use the RTK Query DevTools monitor much, but I'd agree that it would probably be useful to focus more on the data. I just filed reduxjs/redux-devtools#1126 with that suggestion. Finally, what aspect of the "parallel requests" did you feel was confusing and not straightforward? What did the final result look like? |
Beta Was this translation helpful? Give feedback.
-
Hi, I've been converting a medium size app to use RTK query and I have mixed feelings about it. 90% of the time, RTK query fits my use case well and is very pleasant to work with, but the other 10% of the time, I feel like I'm trying to fit a square peg into a round hole. My main issues:
No easy way to set initial query data. I need to do this in the case of loading user session data from a cookie. Once I load the data from the cookie, I use a saved token to request fresh session data from the server. RTK supports "optimistic updates", which seems like would fit this use case, but it appears that I can only do an optimistic update if I already have cached data, which is impossible in this case.
Related to number 1, user session data is also returned by the response to the account login and register endpoints in my api, so I need a way to add this response to the same user session state slice. It seems that a "pessimistic update" would fit this use case, but again, I cannot do the update if there is no cached data, which of course I don't have if the user isn't logged in.
Because of the above 2 issues, I needed to create another user session state slice without the use of RTK query, that is managed differently than all my other api state, entirely because the RTK query api could not accommodate my use case (which I think is fairly common).
I later ran into another similar issue. I have a page where a user can edit some data and then save it to the server. The first time they edit it, the initial data is generated client side. Then when they make the post request to save the data, the server returns the updated data in the response. The problem is nearly identical to the one above: I need a simple way to set initial query data and then update it based on the server response.
Because of the difficulty I encountered doing this, I'm considering switching to react query which has these features built in:
https://react-query.tanstack.com/guides/initial-query-data
https://react-query.tanstack.com/guides/placeholder-query-data
https://react-query.tanstack.com/guides/updates-from-mutation-responses
Unrelated, I've gotten used to using the "state" tab in the redux dev tools, where I can see the current state at a glance. However, with the rtk query dev tools, I need to click through multiple nested objects in order to get to the
data
field for a query, meaning that I often resort to console.logging thedata
rather than using the dev tools. I realize that technically rtk query saves lots of other types of state, but most of the time, I'm only interested in thedata
that is actually being used in my app.Lastly, there's a part of my app where I need to make multiple parallel requests to the same endpoint. I eventually figured out how to accomplish this in RTK query, but it required writing a couple of helper functions and wasn't straight forward. So perhaps this is not a missing feature per se, but either a built in helper function or an example in the docs would help a lot with this.
I hope this post doesn't come across as too critical. I appreciate the hard work of the redux developers and overall I think rtk query is an excellent library, but the two features that I mentioned would save devs a lot of frustration. Thanks!
Beta Was this translation helpful? Give feedback.
All reactions