server action using shadcn ui forms
Answered
American Chinchilla posted this in #help-forum
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
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
9 Replies
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
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](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
This is already correct
![Image](https://cdn.discordapp.com/attachments/1203116707460423731/1203171744144293938/image.png?ex=65d02001&is=65bdab01&hm=dc084b5f17fa3cd9c69f00543abf3134541ee9c5c3d15bb173b706898b669628&)
and i dont see
async
here![Image](https://cdn.discordapp.com/attachments/1203116707460423731/1203171804835881010/image.png?ex=65d02010&is=65bdab10&hm=88f7dddd9234159a0313adaa779ef177edb9c3c396792940362202e9c258805d&)
so are you sure that errors points towards
TextareaForm
?![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
American ChinchillaOP
I dont see how it should be async
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
Exactly, therefore the error must come from somewhere else?
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
American ChinchillaOP
Also i extracted module to another file and imported in page.tsx and works
Answer
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
Nice! glad to have it working
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
American ChinchillaOP
Yeah thanks anyways