useMutation()
비공식 베타 번역
이 페이지는 PageTurner AI로 번역되었습니다(베타). 프로젝트 공식 승인을 받지 않았습니다. 오류를 발견하셨나요? 문제 신고 →
참고
@trpc/react-query에서 제공하는 훅은 @tanstack/react-query를 간단히 감싼 래퍼입니다. 옵션과 사용 패턴에 대한 자세한 내용은 뮤테이션 문서를 참조하세요.
예시
Backend code
server/routers/_app.tstsximport {initTRPC } from '@trpc/server';import {z } from 'zod';export constt =initTRPC .create ();export constappRouter =t .router ({// Create procedure at path 'login'// The syntax is identical to creating querieslogin :t .procedure // using zod schema to validate and infer input values.input (z .object ({name :z .string (),}),).mutation ((opts ) => {// Here some login stuff would happenreturn {user : {name :opts .input .name ,role : 'ADMIN' asconst ,},};}),});export typeAppRouter = typeofappRouter ;
server/routers/_app.tstsximport {initTRPC } from '@trpc/server';import {z } from 'zod';export constt =initTRPC .create ();export constappRouter =t .router ({// Create procedure at path 'login'// The syntax is identical to creating querieslogin :t .procedure // using zod schema to validate and infer input values.input (z .object ({name :z .string (),}),).mutation ((opts ) => {// Here some login stuff would happenreturn {user : {name :opts .input .name ,role : 'ADMIN' asconst ,},};}),});export typeAppRouter = typeofappRouter ;
tsximport {trpc } from '../utils/trpc';export functionMyComponent () {constmutation =trpc .login .useMutation ();consthandleLogin = () => {constname = '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 >);}
tsximport {trpc } from '../utils/trpc';export functionMyComponent () {constmutation =trpc .login .useMutation ();consthandleLogin = () => {constname = '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 >);}