--- name: safe-action-forms description: Use when integrating next-safe-action with forms -- react-hook-form adapter (useHookFormAction, useHookFormOptimisticAction, mapToHookFormErrors), native HTML forms, bind arguments, or file uploads --- # next-safe-action Form Integration ## Options | Approach | When to Use | |---|---| | `useAction` + native form | Simple forms, no complex validation UI | | `useHookFormAction` (RHF adapter) | Complex forms with field-level errors, validation on change/blur | | `useHookFormOptimisticAction` | RHF forms with optimistic UI updates | ## Quick Start — Native Form ```tsx "use client"; import { useAction } from "next-safe-action/hooks"; import { submitContact } from "@/app/actions"; export function ContactForm() { const { execute, result, isPending } = useAction(submitContact); return (
); } ``` ## Quick Start — React Hook Form Adapter ```tsx "use client"; import { useHookFormAction } from "@next-safe-action/adapter-react-hook-form/hooks"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; import { submitContact } from "@/app/actions"; const schema = z.object({ name: z.string().min(1, "Name is required"), email: z.string().email("Invalid email"), message: z.string().min(10, "Message must be at least 10 characters"), }); export function ContactForm() { const { form, handleSubmitWithAction, action } = useHookFormAction( submitContact, zodResolver(schema), { actionProps: { onSuccess: () => toast.success("Message sent!"), }, } ); return ( ); } ``` ## Supporting Docs - [Native form submission patterns](./form-actions.md) - [React Hook Form adapter in depth](./react-hook-form.md) - [File uploads](./file-uploads.md) ## Entry Points | Package | Entry Point | Exports | |---|---|---| | `@next-safe-action/adapter-react-hook-form` | Default | `mapToHookFormErrors`, types | | `@next-safe-action/adapter-react-hook-form/hooks` | Hooks | `useHookFormAction`, `useHookFormOptimisticAction`, `useHookFormActionErrorMapper` |