Nextjs 14 Form Submit with tRPC

Introdution

In this article,we are gonna enumerate some kinds of form submitting with trpc concretely, globally in detail. If need, please see 《NextJs 14 从入门到精通之tRPC快速入门》

Prepare trpc base enviroment

Form component typically is used in Client Component, you can submmit with http request or call server action directly. So we start along this thought

We need to create tRPC appRouter at first

bun add @trpc/server@next @trpc/client@next

ts 复制代码
import { initTRPC } from '@trpc/server';
import { db } from '@/server/db';

export const createTRPCContext = async (opts: {
  req?: NextRequest;
}) => {
  // get session
  const session = { user: {userId: 'xxx'}, sessionId: 'xxxx' };

  return {
    db,
    session,
    ...opts,
  };
};

const t = initTRPC.context<typeof createTRPCContext>().create()

const publicProcedure = t.procedure;

const protectedProcedure = t.procedure.use(({ ctx, next }) => {
    if (!ctx.session || !ctx.session.userId) {
      throw new TRPCError({ code: 'UNAUTHORIZED' });
    }

    return next({
      ctx: {
        // infers the `session` as non-nullable
        session: {
          ...ctx.session,
          userId: ctx.session.userId,
        },
      },
    });
});

export const appRouter = t.router({
    createUser: protectedProcedure
        .input(z.object({
          name: z.string(),
          password: z.string()
        }))
        .mutation(async ({ ctx, input }) => {
            const { userId } = ctx;
            const data = await ctx.db.user.create({
              data: {...input, userId }
            })
            return { status: 200, data, message: 'success' }
        })
})

Http Request with tRPC

Firstly, make tRPC appRouter match NextJs api-router

ts 复制代码
// src/app/api/trpc/[...trpc]/route.ts
import { fetchRequestHandler } from '@trpc/server/adapters/fetch';
import { appRouter } from '@/server/api/root';
import { createTRPCContext } from '@/server/api/trpc';

const handler = (req: NextRequest) =>
  fetchRequestHandler({
    endpoint: '/api/trpc',
    req,
    router: appRouter,
    createContext: () => createTRPCContext(),
    onError: ({ path, error }) => {
      console.log('------', error);
    },
  });

export { handler as GET, handler as POST };

Secondly, Create tRPC Client with react-query

bun add @trpc/react-query@next @tanstack/react-query

tsx 复制代码
// tRPCReact.ts
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { createTRPCReact } from '@trpc/react-query';
import { unstable_httpBatchStreamLink } from '@trpc/client';
import { useState } from 'react';

const createQueryClient = () => new QueryClient();

let clientQueryClientSingleton: QueryClient | undefined = undefined;

const getQueryClient = () => {
  if (typeof window === 'undefined') {
    return createQueryClient();
  }
  return (clientQueryClientSingleton ??= createQueryClient());
};

export const api = createTRPCReact<AppRouter>();

export function TRPCReactProvider(props: { children: React.ReactNode }) {
  const queryClient = getQueryClient();

  const [trpcClient] = useState(() =>
    api.createClient({
      links: [
        unstable_httpBatchStreamLink({
          url: 'http:localhost:3000/api/trpc',
        }),
      ],
    })
  );

  return (
    <QueryClientProvider client={queryClient}>
      <api.Provider client={trpcClient} queryClient={queryClient}>
        {props.children}
      </api.Provider>
    </QueryClientProvider>
  );
}

Thirdly, used in form component

  • To use react-query, your component must be wrapped with TRPCReactProvider before.
ts 复制代码
export default function RootLayout({ children }) {
    return (
        <html lang="en">
            <body>
                <TRPCReactProvider>{children}</TRPCReactProvider>
            </body>
        </html>
    )
}
  • In form component.
tsx 复制代码
'use client';
import { api } from 'tRPCReact';
export function Form() {
    const create = api.createUser.useMutation({
      onSuccess: () => {}
      onError: (e) => {}
    })
    
    return (
        <form>
            {/* ... */}
            <button onClick={() => create.mutate({
                name, password
            })} >Submit</button>
        </form>
    )
}

Server Action with tRPC's createCaller

NextJs 14 supports calling server action directly for form submmiting. And similarly,tRPC supports server side calls. so we can do following this:

  • Go back to the file of appRouter, export default createCallerFactory
ts 复制代码
// ...
const t = initTRPC.context<typeof createTRPCContext>().create()
// ...

export const createCallerFactory = t.createCallerFactory;
  • Create trpc caller
ts 复制代码
// tRPCServer
const createCaller = createCallerFactory(appRouter);
export const api = createCaller(() => createTRPCContext());
  • Used in form component
tsx 复制代码
'use client';
import { api } from 'tRPCServer';

export function Form() {
    const create = async (formData, FormData) {
        'use server'
        try {
            api.createUser(formData)
        } catch (e) {}
    }

    return (
        <form action={create}>
            {/* ... */}
        </form>
    )
}

Server Action with tRPC's experimental_caller

This is an experimental feature, we should not better to use it. it's based on top of procedure, likely a middleware, finally return a new procedure.

And we will not use the previous defined appRouter. we should re-write the inner logic.

so we can do by following:

  • Export default createCallerFactory
ts 复制代码
// ....
const t = initTRPC.context<typeof createTRPCContext>().create();
// ....
// experimental: form action
export const createActionCaller = protectedProcedure.experimental_caller;
  • Create a file for createActionCaller
ts 复制代码
// tRPCAction
import { experimental_nextAppDirCaller } from '@trpc/server/adapters/next-app-dir';

export const tRPCAction = createActionCaller(
  experimental_nextAppDirCaller({
    createContext: () => createTRPCContext()
  })
);
  • Define a Server Action as a file and write business logic
ts 复制代码
'use server';
import { tRPCAction } from 'tRPCAction';
import { z } from 'zod';

export const createUserAction = tRPCAction
        .input(z.object({
          name: z.string(),
          password: z.string()
        }))
        .mutation(async ({ ctx, input }) => {
            const { userId } = ctx;
            const data = await ctx.db.user.create({
              data: {...input, userId }
            })
            return { status: 200, data, message: 'success' }
        })
  • Used in form component
tsx 复制代码
'use client';
import { createUserAction } from 'actions';

export function Form() {
    return (
        <form action={createUserAction}>
            {/* ... */}
        </form>
    )
}

Summarize

In Next.js 14, using server Actions to submit a form and call tRPC's createCaller is a straightforward way. Both offer different ways to invoke server-side code, depending on your needs and development style. Here is an explanation of the two ways:

  • createCaller is a way to call the server-side tRPC endpoint directly. It is suitable for situations where you need to call the tRPC endpoint directly in server-side code, such as during server Actions or other server-side processing.

  • experimental_caller provides an experimental method for calling tRPC methods on the server side. It is a more straightforward API, but because it is experimental, it may change in future releases

In fact, I suggest to use createCaller, because this way is used directly and once appRouter logic is defined. we can use server side call and server action.

相关推荐
代码小鑫10 分钟前
A031-基于SpringBoot的健身房管理系统设计与实现
java·开发语言·数据库·spring boot·后端
Json____15 分钟前
学法减分交管12123模拟练习小程序源码前端和后端和搭建教程
前端·后端·学习·小程序·uni-app·学法减分·驾考题库
上趣工作室28 分钟前
vue2在el-dialog打开的时候使该el-dialog中的某个输入框获得焦点方法总结
前端·javascript·vue.js
家里有只小肥猫28 分钟前
el-tree 父节点隐藏
前端·javascript·vue.js
fkalis29 分钟前
【海外SRC漏洞挖掘】谷歌语法发现XSS+Waf Bypass
前端·xss
monkey_meng35 分钟前
【Rust类型驱动开发 Type Driven Development】
开发语言·后端·rust
落落落sss43 分钟前
MQ集群
java·服务器·开发语言·后端·elasticsearch·adb·ruby
zxg_神说要有光1 小时前
自由职业第二年,我忘记了为什么出发
前端·javascript·程序员
大鲤余1 小时前
Rust,删除cargo安装的可执行文件
开发语言·后端·rust
她说彩礼65万1 小时前
Asp.NET Core Mvc中一个视图怎么设置多个强数据类型
后端·asp.net·mvc