T
TanStack•3y ago
other-emerald

Keep data from last query is the next query fails

How can i make the "data" to be the last fetched items from the query in case my current query fails? I think the default situation is it returning the initialData.
No description
13 Replies
deep-jade
deep-jade•3y ago
note onError, onSuccess, and onSettled are deprrcated
rare-sapphire
rare-sapphire•3y ago
With "last query", do you mean after the query key has changed?
other-emerald
other-emeraldOP•3y ago
yes!
rare-sapphire
rare-sapphire•3y ago
no, you can keep data while transitioning from one query to the next with keepPreviousData, but after you've fetched data and this query is in error state, you can't display the old data anymore what's the use-case?
other-emerald
other-emeraldOP•3y ago
I guess my question is actually a doubt i have about the cacheing itself... I'll remake it a better way: Let's say i have a useQuery beeing called, with a dynamic filter in the query key. The documentation of RQ says: whenever the query key changes, the query will happen again (unless it's not enable, etc) My point is: If i have the same useQuery (but with a different key, because of the filter), how could i get the previous data fetched for that exact same key? Example: query key: ['test', filterObj] it makes the query again everytime filterObj changes. but if that filterObj was already fetched before, return that fetch result data instead of refetching. So I'm wondering it as if there was an option called "keepPreviousDataFromKey", that if true, everytime the useQuery got an key which was already fetched before, it would get that cache instead of fetching again. I know it seems to not make much sense (since if the queryKey changed, the obvious is to fetch again) but i have some situations where it would be better if the useQuery returned this "previous data from that key" @TkDodo 🔮 I hope I explained this well 😆 I'm not that good at english
rare-sapphire
rare-sapphire•3y ago
Do you just want to set a higher staleTime maybe? So that data is considered fresh longer and thus read from the cache without a background refetch?
other-emerald
other-emeraldOP•3y ago
I tried this, but then new updates on the query key (new filters) won't make the query happen again. @TkDodo 🔮 let's say i have a pagination on a list. Each page means a new query key to my useQuery. If I go back to a page that was already fetched, return 'data' as that fetch's result. If I enter a page that was not fetched, do the query normally. I guess that's the use case I'm going through
rare-sapphire
rare-sapphire•3y ago
That's what setting a staleTime does...
other-emerald
other-emeraldOP•3y ago
In my use case, if my staleTime is Infinity, the query doesnt happen
other-emerald
other-emeraldOP•3y ago
No description
other-emerald
other-emeraldOP•3y ago
It returns my initialData changing filterObj won't make the refetch also
other-emerald
other-emeraldOP•3y ago
@TkDodo 🔮 it actually logs new query instances as i change pages. but no updates at all
No description
rare-sapphire
rare-sapphire•3y ago
Then don't use initialData

Did you find this page helpful?