T
TanStack17mo ago
sunny-green

redirect if user exists

I have a mutation called (user) to sign in user . But i also want the user doesn’t go yo login page ( will be navigated ) if the user in the cache .. how to check for that without doing a request?
1 Reply
extended-salmon
extended-salmon17mo ago
The default behavior of a query is to refetch whenever the component using the query is mounted assuming the query is considered stale. If you set an appropriate staleTime, the cache will be reused without causing the query function to be triggered again. And as for how to avoid triggering the mutation, you could either make it a query (arguably you are fetching an auth token), or you could use onMutate to check the cache before proceeding with the mutation.

Did you find this page helpful?