The symptom#
A section of a page reads a report. The first read fails with a 500, so the section says it could not be read and offers Try again. The person presses it. The message and the button disappear, a loading skeleton takes their place, and if the second read also fails, the error comes back as if nothing happened. The busy state you passed to the button, busy={query.isFetching}, never shows, because the button is gone before it would.
A query that already had data behaves differently. After a failed background refresh it stays in the error status while it refetches, and it keeps its data. So the bug shows up only on reads that failed the first time, which is also when a Try again matters most.
Why the query goes back to pending#
Version 5 renamed the status that means "no data yet" from loading to pending, as the v5 migration guide describes. The status answers one question: is there data? The queries guide defines pending as "The query has no data yet", and a separate field, fetchStatus, says whether a request is running. The same guide suggests checking isPending, then isError, then rendering the data, which is the pattern that loses the error during a retry.
When a fetch starts on a query whose data is still undefined, the query resets its status to pending and its error to null. It does not do that for a query that holds data, which keeps status: "error" and its error during the refetch. The reasoning is that the status describes the data, and a query without data is again waiting for its first answer. The consequence for a UI is that isError turns false for the length of the retry.
The lab#
The lab needs Node.js and @tanstack/query-core, and runs in a second. It prints the observer's result every time it changes.
// retry.mjs
import { QueryClient, QueryObserver } from "@tanstack/query-core";
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const client = new QueryClient({ defaultOptions: { queries: { retry: false } } });
async function run(label, answers) {
console.log(`--- ${label}`);
const observer = new QueryObserver(client, {
queryKey: [label],
queryFn: async () => {
await sleep(20);
const answer = answers.shift();
if (answer === "fail") throw new Error("HTTP 500");
return answer;
},
});
let last = "";
observer.subscribe((r) => {
const line = `status=${r.status} fetchStatus=${r.fetchStatus} error=${r.error?.message ?? null} data=${JSON.stringify(r.data ?? null)}`;
if (line !== last) console.log(line);
last = line;
});
await sleep(50);
if (label === "had-data") {
await observer.refetch(); // second answer: fail
}
console.log("> refetch()");
await observer.refetch();
observer.destroy();
}
await run("never-answered", ["fail", { rows: 3 }]);
await run("had-data", [{ rows: 3 }, "fail", { rows: 4 }]);
With @tanstack/query-core 5.108 on Node.js 24, node retry.mjs prints:
--- never-answered
status=pending fetchStatus=fetching error=null data=null
status=error fetchStatus=idle error=HTTP 500 data=null
> refetch()
status=pending fetchStatus=fetching error=null data=null
status=success fetchStatus=idle error=null data={"rows":3}
--- had-data
status=pending fetchStatus=fetching error=null data=null
status=success fetchStatus=idle error=null data={"rows":3}
status=success fetchStatus=fetching error=null data={"rows":3}
status=error fetchStatus=idle error=HTTP 500 data={"rows":3}
> refetch()
status=error fetchStatus=fetching error=HTTP 500 data={"rows":3}
status=success fetchStatus=idle error=null data={"rows":4}
The first query goes from error back to pending with error=null while the retry runs. The second, which had data, stays in error and keeps the message.
Keep the error on screen during the retry#
Hold the retry in component state. Set it when the button is pressed, clear it when the refetch settles, and render the error section while either the query has failed or a retry is running:
function ReportSection({ queryFn }) {
const query = useQuery({ queryKey: ["report"], queryFn, retry: false });
const [retrying, setRetrying] = useState(false);
const retry = () => {
if (retrying) return;
setRetrying(true);
query.refetch().finally(() => setRetrying(false));
};
if (query.isError || retrying)
return (
<p>
Could not be read.{" "}
<button aria-busy={retrying} onClick={retry}>Try again</button>
</p>
);
if (query.isPending) return <p>Loading…</p>;
return <p>{query.data.rows} rows</p>;
}
The early return also makes a second press start nothing while the first retry runs. A Vitest and Testing Library test rendered this component and a naive one that checks only isPending and isError. Each test failed the first read, pressed Try again, read the page while the retry was in flight, then answered the retry:
naive, retry in flight: "Loading…"
naive, retry answered: "3 rows"
fixed, retry in flight: "Could not be read. Try again"
fixed, retry answered: "3 rows"
The order of the two checks matters. If isPending comes first, the fixed component would show the skeleton during the retry as well.
Two other options exist, each with a cost. The result's errorUpdateCount stayed at 1 during the retry in the lab, so isPending && errorUpdateCount > 0 can tell a retry from a first load without local state, but it cannot tell whether this component started the retry. Making the query keep placeholder data avoids pending entirely, but it shows rows the person never asked for.
Which errors deserve a Try again#
A retry helps only when the failure can pass: a timeout, a 500, a dropped connection. A 401, 403 or 404 will answer the same again, so show its message without a button, and remove any rows the section showed before the refusal. The unhandled error guide covers where an error like this should be handled at all.
Where this came from#
Proofline found this in its own console, in a section of the review page that reads a release compatibility check, and shipped the fix in its production release of 1 December 2026. The section told the reader to try again but offered no control to do it. A Try again button built on isError would have vanished on the first press, so the shipped version keeps a local retry flag, ignores a second press, and offers no retry for 401, 403 or 404 answers. Two new component tests cover a failed read followed by a pending retry and a refusal after rows had loaded. Proofline reviews GitHub pull requests; new organizations get one calendar month of hosted code review without a cloud connection. Start a free GitHub review · Eligibility and fair-use terms
Questions#
Did TanStack Query v4 behave the same way?
The status was called loading in v4, and the rename did not change what it means: no data yet. Check your version's behavior with the lab before relying on either.
Why not just render the error when failureReason is set?
failureReason is reset when a new fetch starts, just like the error, so it is empty during the retry too.
Does retry: 3 on the query avoid the problem?
It retries automatically before the query reports the error, which delays the error but does not change what happens when a person presses Try again afterwards.