Skip to content

keepPreviousData doesnt keep Error between refetch #6290

@matzxrr

Description

@matzxrr

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

  1. Create a useQuery with keepPreviousData and a queryFn that errors on first request.
  2. 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions