T
TanStack2y ago
correct-apricot

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
metropolitan-bronze2y 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-apricot
correct-apricotOP2y 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:
queryCache = new QueryCache();

clipObj.oldData = queryCache.find(['clips', { type: 'status' }, clipProps.data.nodeId, columnKey])
queryCache = new QueryCache();

clipObj.oldData = queryCache.find(['clips', { type: 'status' }, clipProps.data.nodeId, columnKey])
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
helpful-purple2y ago
React 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
helpful-purple2y ago
I'm just guessing here but I think you can ignore creating one and just do queryClient.getQueryCache().find(...)
correct-apricot
correct-apricotOP2y 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
metropolitan-bronze2y ago
you just want const oldData = queryClient.getQueryData(key)
correct-apricot
correct-apricotOP2y ago
That was it. Thank you!

Did you find this page helpful?