Use React Hook Form
Build a React Hook Form form with our Field parts. Schema checks, server errors, and a submit button that waits.
Forms · step 3 of 3 · 6 min read
Prompt for your agent
Your agent reads /docs/forms/react-hook-form.md: the same text as this page, as Markdown.
You get four full files to copy: one field, a full form with server checks, two other control types, and a list of rows. Each file uses React Hook Form for the values and our Field parts for the look.
Before you start:
- Add the items:
npx shadcn@latest add @gelatin-ui/form @gelatin-ui/field @gelatin-ui/button @gelatin-ui/input. Add the other controls that a file imports (select,switch,input-group) the same way. - Add the packages:
npm install react-hook-form @hookform/resolvers zod. The files use Zod 4. - Write the server calls that the files import, or replace them with yours:
subscribein@/lib/newsletter, andinvite,isMember,inviteAll, andupdateMemberin@/lib/team. Each takes the form values and returns a promise.
Anatomy
React Hook Form owns the value and the errors. Controller gives them to Field. invalid turns the field red. FieldError with match shows the message.
File: newsletter-form.tsx.
Build a form
Pass your schema through zodResolver. By default, the form checks on submit first. After that, it checks on each change, so an error goes away when the value is correct.
File: invite-form.tsx.
zodResolver comes from @hookform/resolvers. That package also has resolvers for Valibot, ArkType, and Standard Schema.
Control types
Every control calls its change callback with the value, not with an event. So you can pass field.onChange from Controller directly, with no wrapper.
File: member-form.tsx. It shows a Select and a Switch.
Show the server's errors
Call setError after the request. The error shows under its field. It goes away when the person changes the value.
See setError in invite-form.tsx, in Build a form.
Wait for the submit
formState.isSubmitting stays true while onSubmit runs. Pass it to the Button busy prop. The Button then shows a spinner, keeps its width, and ignores a second press.
See isSubmitting in invite-form.tsx, in Build a form.
Arrays
useFieldArray gives the rows and two actions. append adds a row, and remove removes a row. Each row is an object with its own id.
File: emails-form.tsx.
Traps
Our own sign-in and settings forms had these bugs first. Each trap has its fix.
- Two checks fight.
Formruns the browser checks beforeonSubmit. Withtype="email", a bad email stops the submit, so React Hook Form never runs. Usetype="text"withinputMode="email", and let the schema check the value. - FieldError needs match. With no
match,FieldErrorshows only the browser checks. Give itfieldState.invalid, the same value asinvalidonField. - Key rows by id. Use
row.idfromuseFieldArrayas the key. With an index key, the values move to the wrong rows when you remove a row in the middle. - Await the request.
isSubmittingstays true only while the promise ofonSubmitruns. Await the request, or the Button stops waiting at once.
Next steps
- Use TanStack Form: the same forms with TanStack Form.
- Field: every part and prop of Field.