Utilizing newData and oldData properties in refetchInterval
I would like to base the number returned by my
refetchInterval
function on whether newData
and oldData
are equal. I have looked at structuralSharing
and the deprecated isDataEqual
options, but it seems like both of these have changing the result of the query in mind. I just want to be able to leverage this property to make a judgment about how fast to refetch. How can I do this? Would I need to provide a function to structuralSharing
and then pass the query into the refetchInterval
function and try to access this property from the query object?7 Replies
metropolitan-bronze•2y ago
There is no "oldData" in refetchInterval. You would have to hang onto oldData by reading from the queryCache in the queryFn and adding it to the next result
correct-apricotOP•2y ago
Is this the same method that is used to set oldData under the hood for structuredSharing and isDataEqual?
I just gave this method a try and the object that I try to assign the value of
queryCache.find([key])
to always comes back undefined (even after the first fetch where it would be expected) despite copying the key verbatim. I've created a new instance of QueryCache as instructed here: https://tanstack.com/query/v4/docs/react/reference/QueryCache
example:
What am I missing?QueryCache | TanStack Query Docs
The QueryCache is the storage mechanism for TanStack Query. It stores all the data, meta information and state of queries it contains.
Normally, you will not interact with the QueryCache directly and instead use the QueryClient for a specific cache.
helpful-purple•2y ago
And gave it to the
queryClient
like seen here?
https://tkdodo.eu/blog/react-query-error-handling#putting-it-all-togetherReact Query Error Handling
After covering the sunshine cases of data fetching, it's time to look at situations where things don't go as planned and "Something went wrong..."
helpful-purple•2y ago
I'm just guessing here but I think you can ignore creating one and just do
queryClient.getQueryCache().find(...)
correct-apricotOP•2y ago
No, I didn't do that. Makes sense then that the context would be wrong.
That was helpful, thanks. After using with queryClient I'm now getting data back.
How do I access the data of the query that's returned? I see there's a promise property that contains the values of the last query (in a [[PromiseResult]] property), but awaiting the promise results in the queries hanging in loading state.
revertState?
metropolitan-bronze•2y ago
you just want
const oldData = queryClient.getQueryData(key)
correct-apricotOP•2y ago
That was it. Thank you!