-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Description
Describe the bug
This bug is related to the keepPreviousData.
I have found that useQuery doesn't start tracking previous data until the query has returned with successful data from the queryFn.
In the event that queryFn encounters an Error on its first query, This error does not persist during a refetch.
If the useQuery call previously had a queryFn return with some type of valid data or the useQuery call was given some initialData, errors DO get persisted between fetches as you would expect.
Your minimal, reproducible example
https://codesandbox.io/s/rq-persist-error-first-rsrghk?file=/src/App.js:101-586
Steps to reproduce
- Create a
useQuerywithkeepPreviousDataand aqueryFnthat errors on first request. - Inspect the error value between refetch, it will be
null
export default function App() {
const { error, isLoading } = useQuery({
queryKey: ["badquery"],
queryFn: () => axios.get("https://test.com/thisdoesntexist"),
placeholderData: keepPreviousData,
refetchInterval: 10_000,
retry: false
});
// I would expect the previous error to stay when loading
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<p>{isLoading ? "is loading" : "loaded"}</p>
<p>error: {error?.message}</p>
</div>
);
}Expected behavior
As a user, I expect the error returned from useQuery to persist between refetches when the keepPreviousData option is set to true.
How often does this bug happen?
Every time
Screenshots or Videos
No response
Platform
OS: Windows 11
Chrome: Version 118.0.5993.118 (Official Build) (64-bit)
Tanstack Query adapter
react-query
TanStack Query version
v5.4.3
TypeScript version
No response
Additional context
No response