Why would queryClient.getQueryData() return undefined if the queryCache exists in queryClient.getQueryCache().getAll()? #8080
Unanswered
GarrisonBates
asked this question in
Q&A
Replies: 1 comment
-
we have a plugin for that: https://tanstack.com/query/v5/docs/framework/react/plugins/persistQueryClient |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I created a hook to keep the queryCache in sync with localStorage, so that I can rehydrate from localStorage to offer better offline support for my web app:
However, I'm experiencing what seems to me to be strange behavior. First of all, I added a cache event listener to the queryClient with
queryClient.getQueryCache().subscribe()
, and several"removed"
cache events fire as soon as the loop in my hook completes and the cache is fully populated.In another component,
<CommunityAccounts>
, I want to access the query with queryKey["communityAccounts", 16]
(just as an example). So I have this:For some reason,
communityAccount
andqueryClient.getQueryData(["communityAccount", 16])
print asundefined
until loading is complete (at which point they print normally). However, even on the initial render, the value ofqueryClient.getQueryCache().getAll()
clearly contains the correct queryCache:... So I'm unsure why the other two calls don't print the correct data. How can I address this issue, so that I don't have to wait for the fetch to complete to be able to access the data with my useQuery hooks? The whole purpose of caching and rehydrating from localStorage is to allow users to see data while fetching / with no fetch (if offline), so I'm hoping to figure this out.
Also, any pointers on my hydration logic would be greatly appreciated, I know React Query comes with a built-in
hydrate
anddehydrate
function, but to my understanding that only deals with the entire cache, whereas I'd like to be able to update only the queryCache that's being updated.Thanks so much!
Beta Was this translation helpful? Give feedback.
All reactions