-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
useQuery keepPreviousData is not keeping previous data on error #3046
Comments
nope, this is not how keepPreviousData works. When the query key changes, it will show you the data of the previous query key while you are fetching for the next query key to emulate a "transition". but once the new query key has finished loading, you will just get what that query has. if it's a success state with data, you'll get that, and if it's an error state without data, you'll get that as well. I don't see a good use-case for showing fields from the previous query (like data) mixed with other fields from the current query (like error) after it has settled. |
Thank you for the quick response.
I understand that from your above comment, the data will not be available when an error occurred since the query changed. I am starting to think React Query is not suitable for my use case. By the way, I tried a way to achieve the behavior by setting the latest successful data as initialData through the initialData callback function and at the same time, filter out any error query cache. |
you can of course set initialData from the previous query as data for your "next" query if you want that. That data will then be put into the cache for that page as well, and with the default staleTime of 0, you will get a background refetch fetching the real data. however, if it errors, you will be:
if I would want that behaviour, I would:
It is not technically impossible to return previous data in case of error from react-query perspective, I just think it would be more confusing than helpful (but maybe I'm wrong), as the main use-case was to avoid this:
|
fyi, I just found the bugfix where this behaviour was introduced:
|
Thanks a lot |
However funny that sounds, I support that idea in general. Because right now, Currently, |
I ended up implementing a wrapper hook which does what is expected. Posting here in case someone could also benefit from the code. export const useAppQuery = <
TQueryFnData,
TError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
...args: Parameters<typeof useQuery<TQueryFnData, TError, TData, TQueryKey>>
) => {
const previousDataRef = useRef<TData>();
const result = useQuery(...args);
if (!result.isError) {
previousDataRef.current = result.data;
} else {
result.data = previousDataRef.current;
}
return result;
}; |
@TkDodo I have come across this thread and it is exactly what I am looking for! 🙏 Our use case for react-query is to also cache data for offline use/on poor connection. When data currently fails to load (even if its cached in a persistor) onError as clarified above, the cached data gets replaced with only the error. This is not ideal because it is overriding data that we are persisting on purpose and react-query is acting as our store. We want to keep old data in cache until more up to date data is retrieved. I would think other people may come across this use case too since this library seems to be trying to remove a lot of redux/store use cases Do you see any other way that this could be solved? Thanks in advance! |
please show a codesandbox reproduction. Generally, we don't remove data if it's there. When you get an error for a refetch while you have data, you will be in |
Thanks so much for your reply! The data is defined onError but when the hook is destroyed, the next time the hook is used, the data is not there anymore. I will try to somehow create a codesandbox reproduction however its hard because especially now that you mention it, the behaviour probably has to do with persistence or hook re-instantiation. Do you have any ideas what it could be? |
queries in see: https://tanstack.com/query/v4/docs/react/reference/hydration#limitations |
@siteassist-benjamin |
Thank you so much @TkDodo doing Really appreciate the help ❤️ |
just keep in mind that that means that you can be in
you will get a runtime error. |
useQuery used with keepPreviousData as true is not keeping the previous query data when an error occurs in a paginated list fetch.
Steps:
Expected behavior
When the second fetch failed, I am expecting the data from the first fetch to be kept for showing in the UI.
Intention:
Keep the data from last successful fetch and show a error toast for the failure
Screenshots
![image](https://user-images.githubusercontent.com/38159830/144643557-abc9b1dd-4227-45d6-b7cd-0988abbf9048.png)
Desktop (please complete the following information):
Additional context
I referred to the stackoverflow thread and seen a comment that the data will not become undefined .
Stackoverflow link
The text was updated successfully, but these errors were encountered: