useMutation()
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.
Works like react-query's mutations - see their docs.
Example
Backend code
tsx
import { trpc } from '../utils/trpc';export function MyComponent() {// This can either be a tuple ['login'] or string 'login'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.isLoading}>Login</button>{mutation.error && <p>Something went wrong! {mutation.error.message}</p>}</div>);}
tsx
import { trpc } from '../utils/trpc';export function MyComponent() {// This can either be a tuple ['login'] or string 'login'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.isLoading}>Login</button>{mutation.error && <p>Something went wrong! {mutation.error.message}</p>}</div>);}