Replies: 2 comments 7 replies
-
You will have to at some point update that list - RTK Query is a "document cache" and even if you call that You can do that manually with the technique described in the docs as "optimistic update", but I'd rather really recommend to re-fetch that list by invalidation. Honestly, my question would be why you fetch so many items - just fetch what you need for displaying the list? You will need to filter and sort on the server anyways since at some point that list will definitely outgrow the "deliver all items to the browser" approach. |
Beta Was this translation helpful? Give feedback.
-
Looking at a question of someone else here I think what would help is if the query hooks, like the mutation hooks, would give a trigger function that would allow to send the request upon an event!! |
Beta Was this translation helpful? Give feedback.
-
I have a question about what seems to me like a common use case without a clear answer in the docs.
Lets assume we have an app that shows a list of posts.
We have a component called PostsList that subscribers to an endpoint getPosts.
It gets all posts and renders the PostDetails components and provides them the individual posts as props.
So far the PostDetails doesn't need to subscribe to any endpoint. Now, each PostDetails component can also update the individual post it is rendering. After triggering the mutation the individual post needs to be refetched, but not the entire list (which can contain tens of thousands of posts).
The PostDetails component can subscribe to the getPostById endpoint but this would mean each time the component is rendered, this endpoint will be called, however, this is not needed because as I said the PostDetails component normally gets the post data as props from the PostsList component. It is also unwanted because we have lots of posts and there is no need to send individual getPostById queries. It's covered by the one call to getPosts...
So the question is how to achieve this nicely.
What I did is this - I added a conditional call to the getPostById in the PostDetails (using the skip option).
I have a local skip state in the PostDetails component.
By default it is skip: true. After the mutation is triggered (the user updates a field of one of the posts) I wait for the query to succeed (by calling unwrap on it) then set the skip to false.
The PostDetails component checks if it has data from the getPostById endpoint, if so use that data for the render otherwise take the post data from the props.
In the api definition of corse I set provides Tags and invalidates tags, so the mutations will invalidate the cache for the post they update.
What I have achieved is that on first render we use the data frpm the props and avoid sending individual getPostById requests, but after the first mutation we do rely on the getPostById after each mutation to fetch the new data and update the UI.
I think this is a common use case but I didn't find an answer in the docs what is the preferred way to approach it.
The selectFromResult option doesn't seem to help me here either because 1. I can just send the individual post data as props, and 2. It still remains open how I cause the individual post to be refetched after the update post mutation.
I would be glad for any feedback.
Thanks,
Lior.
Beta Was this translation helpful? Give feedback.
All reactions