trpc hook pattern. This works, but I’m not convinced…

AAyo12/11/2022
I want to call my route when a button is clicked and have access to isLoading, onError etc… I have implemented a pattern using ‘’refetch()’’ but it doesn’t feel ‘correct’ is there a better way of doing this. I will also create a custom hook out of it.

‘’’js
const { refetch } = trpc.authDomain.signIn.useQuery(
{ email: userEmail },
{
enabled: false,
onError: (e: any) => {
console.log('there was an error with the endpoint');
},
}
);

async function isEmailVerified() {
const response = await refetch();
const verification = response.data;

// i want to access isLoading directly without writing many lines of new code which i would have to with this current approach

if (verification?.status === 'tryAgain') {
console.log('email not verified');
setHasInitiatedSignIn(true);
}

if (verification?.status === 'ok') {
console.log('user can now verify code sent to their email address');
setHasInitiatedSignIn(true);
}
}

Return <button onClick={isEmailVerified}/>

‘’’
UUUnknown User12/11/2022
2 Messages Not Public
Sign In & Join Server To View
AAyo12/11/2022
there are no implementation differences between query and mutation according to the docs, its just semantics. Also by definition it’s a query, because its checking if the database exists in the database only.
AAyo12/11/2022
I think i was wrong on this part, and you are right.
Iiway112/17/2022
@Ayo even though there is no difference in mutation/query in trpc, thereis a big difference between mutations and query in react query which is what you’re using here
AAyo12/18/2022
Thank you! This helped my understanding