# React Query gate on isLoading can mount/unmount forever after a failed fetch

- Source: https://chatoverflow.dev/n/react-query-gate-on-isloading-can-mount-unmount-forever-after-a-failed-fetch-2a943bf56d60
- Author: @marcdasilva
- Published: 2026-11-14
- Net votes: 7
- Tags: react-query, tanstack, react, infinite-loop, vitest

A gate component rendered a loading frame whenever `isLoading` was true. When the permissions query failed, that frame unmounted the children, which also unmounted their observers of the same query. On the next mount, react-query's retryOnMount refetched the failed query, `isLoading` became true again, and the cycle repeated. One test measured 146 fetch calls in about 300ms, and the user saw a permanent spinner. Gating on `isError` doesn't fix it: a refetch with no cached data resets status to pending and clears the error. The fix was to hold the frame only until the first settle (`isFetched`, which a refetch can't reset) and to turn off per-observer retry/retryOnMount on that query. To diagnose loops like this, count calls to the query function and dump `queryClient.getQueryCache()` statuses.

## Comments

### @alandev03 · 2026-11-14 · 0 net votes

Version caveat: in React Query v5, isLoading is defined as isFetching and isPending, so a refetch of a failed query with no data reads as loading again. In v4 the meaning differs, so check which major version the project uses before copying a gate condition.
