Replies: 2 comments 2 replies
-
I am not sure I understand the problem, but have you tried setting cacheTime to infinity? |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a very interesting use case for which I haven't found a clean solution with React Query.
I'm building a calendar with the following custom query hook:
The data returned by the backend looks like this:
In the calendar component, I'm keeping track of selected posts and I need to display them in a separate sidebar. The selected posts from a given month can still be displayed in the sidebar even if the user has navigated to a different month.
My attempt to solve this involves normalizing the posts data into their own individual caches with the
['calendar-posts', post.id]
query key. Then in the sidebar, I implemented auseSelectedPosts
hook that usesQueriesObserver
to subscribe to the selected posts (without a query function) and get the data.This seemed to work well at first. The subscriptions for the selected posts kept the caches active, and prevented them from being garbage collected even after navigating to other months in the calendar.
However, there's still a problem. Those caches are seeded when the calendar endpoint is called, but after 5 minutes, the posts that are not selected are garbage collected from the "calendar-posts" caches and if at that point I try to select a post from the current month, the
useSelectedPosts
hook can't find them in the caches and the sidebar breaks.So I'm back at square one.
I don't see any other way to implement this use case without manually normalizing the posts data in the cache. How can I prevent it from being garbage collected in the scenarios I describe above? Am I going at this the wrong way?
--
Some workarounds I've tried:
Create a
useKeepCalendarPosts
hook that subscribes to all normalized "calendar-posts" that are currently visible in the calendar (I just pass the posts array I get fromuseCalendarData
to this hook).Tried with
QueriesObserver
again here, but performance screeched to a halt.Then tried with
useQueries
, and it works......but I'm passing a query function that returns undefined and I'm worried that that might screw the cache in some edge case. Also, this feels hacky.
Tried setting the
refetchInterval
on theuseCalendarData
hook to less than 5 minutes so that the "calendar-posts" are always refreshed, but this would cause performance problems in the server (getting this calendar data is very expensive).Any other ideas?
Beta Was this translation helpful? Give feedback.
All reactions