Bennett McElwee
Bennett McElwee

Reputation: 25790

Can a useQuery hook skip itself?

Instead of skipping a query by passing skipToken to its useQuery hook, is is possible for the query to skip itself?

In the RTK Query API Reference section Setting default headers on requests, there is an example where prepareHeaders checks the store for an auth token, and if it is there then it sets it as the Authorisation header for the request. This is exactly what I am doing in my app.

But if the token is not in the store then the request would fail with HTTP 403. Even worse, that failure is cached by RTKQ so the useQuery hook will fail again next time, even if the auth token has become available in the meantime.

So in that case I want to query to be skipped. I can only see 2 ways to do this, and neither is satisfactory:

I would like a way for the query to be able to be able to skip itself if the auth token is not present (as if skipToken has been passed into the useQuery hook). Is it possible to do something like this?

Upvotes: 0

Views: 1508

Answers (1)

phry
phry

Reputation: 44196

No, there is no such mechanic of skipping itself.

I'd suggest adding an authenticated tag to all your queries that need authentication and just invalidating that tag from your login mutation.

Upvotes: 1

Related Questions