limaho33
limaho33

Reputation: 25

React-query Global enabled/disabled hooks

I am working on a react native app using react-query. We have 11 query hooks that retrieve information from our server. 3 of those hooks can run without auth tokens, but the rest needs an auth token. Currently each hook has different ways to be enabled: !!authToken or a different condition. No problem with this.

However when a user signs out our logs indicates that all queries are being triggered, We are using the below code into a signout hook.

queryClient.clear() queryClient.removeQueries()

Also is it a good practice to create a new queryClient when a new token is generated, that way we can enable most of the queries from global config?

Thanks in advance

Upvotes: 0

Views: 671

Answers (1)

Abe
Abe

Reputation: 5508

.clear and .removeQueries are both ways of clearing the cache. Clearing the cache causes a refetch - the query has been made, and now there's no data for it in the cache, so it has to go get the data again.

I recommend using queryClient.resetQueries({}, { cancelRefetch: false }) instead. This takes your queries back to their initial state, and cancelRefresh: false specifies that any inflight queries will not be refetched.

Upvotes: 0

Related Questions