Call post.hello on button click
Hi, I know that trpc is using React useQuery under the hood in T3 on the client and therefore can only be used in a React component body. But, I am wondering if there is a way to modify the router call a trpc procedure on a button click in a client component.
I realize that I can make a fetch request to
http://localhost:3000/api/trpc/post.test
, but this defeats the point of TRPC.4 Replies
your trpc router should look something like
hello: publicProcedure.input(...).mutation(()=>{...})
and the usage in your component looks something like
thanks @andersgee, that did allow me to call the procedure on the button click. However, when I try to return data from the mutation, the return type is always void, even when I specify the output like so.
I cannot log the output
The procedure is definitely called though. I see "calling server" in the terminal
The output will be in
useMutation({onSuccess: (x)=>
gotcha. Ok I need to read up on React Query. Thanks so much @andersgee