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.
import { api } from "~/trpc/react";
...
const handleHello = async () => {
const hello = await api.post.hello.query({ text: "from tRPC" });

};
....
<button onClick={handleHello}>Hello</button>
import { api } from "~/trpc/react";
...
const handleHello = async () => {
const hello = await api.post.hello.query({ text: "from tRPC" });

};
....
<button onClick={handleHello}>Hello</button>
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
andersgee
andersgee5mo ago
your trpc router should look something like hello: publicProcedure.input(...).mutation(()=>{...}) and the usage in your component looks something like
const postHello = api.post.hello.useMutation()
...
<button onClick={()=>postHello.mutate(...)}>
const postHello = api.post.hello.useMutation()
...
<button onClick={()=>postHello.mutate(...)}>
james162861
james1628615mo ago
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.
hello: publicProcedure
.input(z.object({ text: z.string() }))
.output(z.object({ greeting: z.string() }))
.mutation(({ input }) => {
console.log("calling server ");
return {
greeting: `Hello ${input.text}`,
};
}),
hello: publicProcedure
.input(z.object({ text: z.string() }))
.output(z.object({ greeting: z.string() }))
.mutation(({ input }) => {
console.log("calling server ");
return {
greeting: `Hello ${input.text}`,
};
}),
I cannot log the output
const handleStart = () => {
const result = postHello.mutate({ text: "from tRPC" });
console.log( result);
};
const handleStart = () => {
const result = postHello.mutate({ text: "from tRPC" });
console.log( result);
};
The procedure is definitely called though. I see "calling server" in the terminal
andersgee
andersgee5mo ago
The output will be in useMutation({onSuccess: (x)=>
james162861
james1628615mo ago
gotcha. Ok I need to read up on React Query. Thanks so much @andersgee
Want results from more Discord servers?
Add your server
More Posts
TRPC Security/ Authorization ConcernsHey guys, I'm a bit worried the default protected procedure isn't enough to secure data in my app. Aoptimistic updates tRPC v11 + TanStack Query v5I am reading https://tanstack.com/query/latest/docs/framework/react/guides/optimistic-updates#if-thenextjs all pages are dynamicI am using create t3 app with app router created like ~3/4 months ago. This implementation makes allThe HTML Dialog Element vs Headless Component Libraries DialogHi guys, i wanna learn why Headless component libraries like Ark UI/Radix Ui/etc,etc for a Dialog coDynamically set page title in app router (RSC)How do I set the browser title dynamically (say using some data from supabase that runs on server siAdding fields to next-auth session object with module augmentationI'm trying to add an additional field to the next-auth session object. Having looked at the prisma lBest way to get a react component's raw code?Is it possible to get a component's raw code? For example. ```ts function Post() { return ( <getServerAuthSession returns null when using CredentialsProvideri just started a new t3 app using the app router. i added just this to the providers array in the `ahttp://localhost:3000/api/auth/callback/discord redirect_uri is not working for create t3 turboAm I missing something? The only thing I am getting is an error telling me InvalidCheck: Missing stIs there a cleaner way to use server actions with Zod and keep the pages as independent as possible?https://github.com/pieter-berkel/nextjs-structure/tree/master/app/posts/%5Bid%5D/edit