Next.js Discord

Discord Forum

server action using shadcn ui forms

Answered
American Chinchilla posted this in #help-forum
Open in Discord
Avatar
American ChinchillaOP
hey can someone tell me how to use a server action in shadcn ui form component?
"use client"

import { zodResolver } from "@hookform/resolvers/zod"
import { useForm } from "react-hook-form"
import { z } from "zod"

import { Button } from "@/components/ui/button"
import {
  Form,
  FormControl,
  FormDescription,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from "@/components/ui/form"
import { Textarea } from "@/components/ui/textarea"
import { addData } from "@/lib/actions"

const FormSchema = z.object({
  bio: z
    .string()
    .min(10, {
      message: "Bio must be at least 10 characters.",
    })
    .max(160, {
      message: "Bio must not be longer than 30 characters.",
    }),
})

export default function TextareaForm() {
  const form = useForm<z.infer<typeof FormSchema>>({
    resolver: zodResolver(FormSchema),
  })

  async function onSubmit(data: z.infer<typeof FormSchema>) {
    await addData(data)
  }

  return (
    <Form {...form}>
      <form onSubmit={form.handleSubmit(onSubmit)} className="w-2/3 space-y-6">
        <FormField
          control={form.control}
          name="bio"
          render={({ field }) => (
            <FormItem>
              <FormLabel>Bio</FormLabel>
              <FormControl>
                <Textarea
                  placeholder="Tell us a little bit about yourself"
                  className="resize-none"
                  {...field}
                />
              </FormControl>
              <FormDescription>
                You can <span>@mention</span> other users and organizations.
              </FormDescription>
              <FormMessage />
            </FormItem>
          )}
        />
        <Button type="submit">Submit</Button>
      </form>
    </Form>
  )
}
i tried this example and in onSubmit i called the server action

"use server"

import prisma from "./db"

export async function addData(data: any) {
  await prisma.guild.update({
    // some code here
  })
}
Answered by American Chinchilla
Also i extracted module to another file and imported in page.tsx and works
View full answer

9 Replies

Avatar
American ChinchillaOP
and im getting Error: async/await is not yet supported in Client Components, only Server Components. This error is often caused by accidentally adding'use client'to a module that was originally written for the server.
Avatar
Alfonsus Ardani
This is already correct
Image
and i dont see async here
Image
so are you sure that errors points towards TextareaForm ?
Avatar
American ChinchillaOP
I dont see how it should be async
Avatar
Alfonsus Ardani
Exactly, therefore the error must come from somewhere else?
Avatar
American ChinchillaOP
Also i extracted module to another file and imported in page.tsx and works
Answer
Avatar
Alfonsus Ardani
Nice! glad to have it working
Avatar
American ChinchillaOP
Yeah thanks anyways