-
Hey everyone, TLDR; my solution: https://codesandbox.io/embed/github/fnn/react-query-list-details-example/tree/csb-mmy1f My problem is that the list query (['todos']) is separated from the details query (['todo', 1]) and if I mutate the selected item in the details component the same list item gets not updated. So for simple lists I think the way to go is to invalidate the list query but if the list is a frequently changing paginated API then this could lead to weird behavior for the user (Edit one item on page 5 => invalidate list query => list completely change because you edit one item). My solution is to use the list query only for the list order/structure and each list item would use a separated query with infinite stale time. The list fetch function sets each item to the cache. Works ok, but maybe there is a better way. Here a CSB with the problem: Here a CSB with my solution: EDIT: |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
You could:
this can even be done with one statement if you use the smart matching, but they would all have to start with the same key (e.g.
|
Beta Was this translation helpful? Give feedback.
-
This is what I do right now in production: I have a custom hook (let's call it useArticles) to get the list to show in the sidebar, the list doesn't have all the info, only the needed to be displayed in the list (e.g. it doesn't have the whole content). I have another custom hook (let's call it useArticle) to get a single item to show in the content area, this has the whole information including the content which can be large. When I access I fetch the list, when the user access one of the articles I fetch that article, additionally I define initialData in the useArticle to read all the info I can from the list (titles, dates, etc.) so I can show that to the user while the content is loading (this helps to the UX). Then, when something is updated in one of the articles I trigger a revalidation of the list and the individual article, this way the list will always be updated with that current info of each item. |
Beta Was this translation helpful? Give feedback.
-
I have similar scenario of list and deatails. On click of list time, event details page is opened and for this I have another hook useEventDetails with key [{scope: 'events', entity: 'details', id: 'some-guid'}] I am editing the Event and using optimistic approach to update the event.
|
Beta Was this translation helpful? Give feedback.
You could:
this can even be done with one statement if you use the smart matching, but they would all have to start with the same key (e.g.
todos
andtodos, selected
:onMutate
function, and then rollback on error / invalidate on success. see theoptimistic update
example in the docs.