Replies: 7 comments 36 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.
-
@markerikson Just a clarification/explanation of what I'm currently doing and trying to do: 😄
As for what I am expecting of help by posting here, that's easy to answer: I am expecting nothing. 😄 It's just that when I am working on stuff I tend to mutter to myself when I am working, in whatever forum is available, because formulating things as questions and problems as text helps me fix the problem. I often see the solution as I'm typing a question, and then I end up sending the post anyway (since I've spent so much time typing it). I am aware (because I have been told so earlier in other places), that is is something people may find annoying, so if it is, I will take my muttering to myself elsewhere. (with no offense taken 😄) But please note that at least twice in this particular muttering your comments (and the comments of @EskiMojo14 ) have taken me out of a blind alley and/or restarted the conversion when I was ready to call complete. Thanks for that! And if I go to muttering to myself I would like to post a final message when I figure out where I went wrong (which may be just forgetting to dot and i or stroke a t somewhere (metaphorically)), because I really hate it when I google up someone who as the exact problem and there is no solution to be found. So I try hard not to be that guy... 😄 (anyway: unless I hear nothing back I'll go back muttering to myself offline and just post the final solution here) |
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