UNPKG

@trpc/next

Version:

The tRPC Next.js library

76 lines (56 loc) • 2.05 kB
# useMutation() > Call mutations with the classic trpc.procedure.useMutation() hook From https://trpc.io/docs/client/react/useMutation > [!WARNING] Deprecated > The classic React Query integration (`@trpc/react-query`) only gets bug fixes, and will be removed in the next major version of tRPC. New projects should use the [TanStack React Query integration](https://trpc.io/docs/client/tanstack-react-query) (`@trpc/tanstack-react-query`). To move an existing project, follow the [migration guide](https://trpc.io/docs/client/tanstack-react-query/migrating). > [!NOTE] > The hooks provided by `@trpc/react-query` are a thin wrapper around @tanstack/react-query. For in-depth information about options and usage patterns, refer to their docs on [mutations](https://tanstack.com/query/v5/docs/framework/react/guides/mutations). ### Example <details> <summary>Backend code</summary> ```tsx no-check title="server/routers/_app.ts" import { initTRPC } from '@trpc/server'; import { z } from 'zod'; export const t = initTRPC.create(); export const appRouter = t.router({ // Create procedure at path 'login' // The syntax is identical to creating queries login: t.procedure // using zod schema to validate and infer input values .input( z.object({ name: z.string(), }), ) .mutation((opts) => { // Here some login stuff would happen return { user: { name: opts.input.name, role: 'ADMIN' as const, }, }; }), }); export type AppRouter = typeof appRouter; ``` </details> ```tsx no-check import { trpc } from '../utils/trpc'; export function MyComponent() { const mutation = trpc.login.useMutation(); const handleLogin = () => { const name = 'John Doe'; mutation.mutate({ name }); }; return ( <div> <h1>Login Form</h1> <button onClick={handleLogin} disabled={mutation.isPending}> Login </button> {mutation.error && <p>Something went wrong! {mutation.error.message}</p>} </div> ); } ```