Setup
I have the following react-query hook:
const STALE_TIME_MS = 12 * 60 * 60 * 1000; // 12h
const useUser = () => {
const services = useServices();
const { isLoading, error, data } = useQuery(
'user',
() => services.user.verify(),
{
staleTime: STALE_TIME_MS,
cacheTime: STALE_TIME_MS,
}
);
return {
isLoading,
error,
user: data,
};
};
Then, I have an App component as a child lives inside the QueryClientProvider:
const App = () => {
const { isLoading } = useUser();
if (isLoading) {
return <Authenticating />;
}
return (
<HashRouter>
<Switch>
<Route path="/login" component={Login} />
<Route path="/" component={Dashboard} />
</Switch>
</HashRouter>
);
}
The Login component checks if the user is already logged in and redirects it back to /:
const Login = () => {
const { user } = useUser();
if (user) {
return <Redirect to="/" />;
}
...
}
The Dashboard component does the reverse:
const Dashboard = () => {
const { user } = useUser();
if (!user) {
return <Redirect to="/login" />;
}
...
};
Problem
When the request that services.user.verify() makes fails, react-query keeps retrying the user query indefinitely. This is despite the fact that react-query's default retries is 3.
The problem seems to come from the fact that I am calling useUser() from multiple components that are mounting/unmounting. When I remove the redirect logic from Dashboard and Login, react-query retries 3 times and then takes me to the login screen as expected.
The interesting fact is that the user query is marked as stale. If I change my services.user.verify() implementation to return null instead of throwing an error when the user is not authenticated, the query is marked as fresh and I don't have this problem at all.
Questions
- Does
react-queryalways consider queries stale when they fail even after the retries? - Does a new component calling the query always result in a retry?
- Is there an option to prevent this from happening?

