Replies: 6 comments 33 replies
-
Here is a similar question on stackoverflow: https://stackoverflow.com/questions/78994431/how-to-trigger-data-fetching-from-an-action-with-rtk-query I have to admit none of the answers looked very attractive (for the same reasons the person posing the question didn't like them, probably...? "useEffect"...? The existence of "useEffect" is one reason I'm sticking with redux....). There are actually two things I find myself missing:
|
Beta Was this translation helpful? Give feedback.
-
This has promise, maybe...? https://www.reddit.com/r/reactjs/comments/ruekqt/comment/hqylrjw/ The lazy query option looks complicated... The conditional fetching sounds promising, but I couldn't figure out where conditional fetching was, in the example linked to...= |
Beta Was this translation helpful? Give feedback.
-
I gotta be honest, I don't understand the question: You can use |
Beta Was this translation helpful? Give feedback.
-
Answering my own question here: the RTK query way to achieve the same effect is to use a mutation and then use either onQueryStarted or onCacheEntryAdded to either route the mutation POST's return value into a reducer or trigger a reload of the data using the GET query (I haven't figured out quite how and what yet. Need to figure out what "caches" and "tags" means in this context...?). (I hadn't gotten to mutations when I started this thread. I started in one end replacing the initial GET operations that populate the app, and now I've come to the first operation that makes a change) Side note to the RTK people: the JavaScript tab on the examples in the RTK documentation is currently non-functional. I don't know if this is intentional or not, but it makes it harder for people like me (JavaScript users) to use the docs. I would be very happy is this is fixed. 🙏 |
Beta Was this translation helpful? Give feedback.
-
Note that I haven't been able to use the above information to handle routing of return from updates yet, but this crateApi created matchers solved a different issue for me: #4778 (comment) One thing I'm wondering: If a reducer uses a createApi created matcher to store the results of a query or a mutate, is useSelector() of that reducer's value in a component enough to trigger reloads of that value? Or do I still need to use the useBlahBlahQuery() hook to make the HTTP loading go? |
Beta Was this translation helpful? Give feedback.
-
Hm.... I'm running @reduxjs/toolkit 2.2.3 from April 2024 and react 18.2.0 from September (what?) 2022. Some upgrades are probably in order, but what will break in redux-first-history...? (which is kinda fragile). Not sure where to start upgrading, but I will try waiting with the new react major version and just try bumping @reduxjs/toolkit first (and if that breaks, bite the bullet and upgrade everything). |
Beta Was this translation helpful? Give feedback.
-
One thing I do in redux-and-saga is to piggyback updated values on an HTTP POST to update a value.
It's not REST, strictly speaking. I.e. I don't return the object I'm updating.
Instead I usually return the parent of what I'm updating and "land" it in the same reducer as the data is fetched into with the regular HTTP GET. Then everything is automatically updated. E.g.
(BUTIKKER_MOTTA is the result of an HTTP GET for a list of stores, NYBUTIKK_LAGRET is the result of an HTTP POST to create a new store, and contains the complete list of stores, and BUTIKK_LAGRET is the result of an HTTP POST to update a store).
The resulting behaviour is quite nice, and, I think, feels quite naturatual.
Anyway: this is simple to do when the reducer results in a simple array (or object) on the top of the redux state.
Not so easy to put something into the reducer of an RTK query (and one probably shouldn't? Probably no end of contracts broken by doing this?)
But the question remains: is there a simple way in RTK query to achieve the same effect?
Failing that: is there a way for an update action to trigger a reload of a different query? (less network efficient since the data has already arrived in the react app, but the visual effect should be the same)
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions