Son
Son2y ago

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

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}/> ‘’’
4 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Son
SonOP2y ago
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. I think i was wrong on this part, and you are right.
isaac_way
isaac_way2y ago
@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
Son
SonOP2y ago
Thank you! This helped my understanding