T
TanStack3y ago
optimistic-gold

When to use queryCache?

Still pretty new to react-query so sorry if this is obvious. I have a pretty nested object I am retrieving from my api, something like 4-5 levels deep, and on the 4th or 5th level there are lets say 10 objects or something. The parent view on the ui I am driving needs bit of all of the data at first which is why I am originally fetching the data all in on go, and then you click on stuff and it takes you to a sub view with some of the nested data. Ideally I'd not like to fetch the data again for outer_object.inner_object. I was under the impression that react-query and it's caching abilities would help prevent that. So if I was to click on something to take me to a subview, could I use a queryCache.find('quer-key', someID) here to get the data from the cache when the component (view) loads - thus accessining the overall larger nested object from the previous page/query? Or am I just still not thinking of react-query properly?
2 Replies
fascinating-indigo
fascinating-indigo3y ago
you'd useQuery again in the lower level, maybe use select to only pick part of the object, and set a staleTime to avoid getting a background refetch every time
optimistic-gold
optimistic-goldOP3y ago
understood. thanks for the response. I'll try and keep that in mind.

Did you find this page helpful?