Skip to content

React Hook Form

Build forms in React using React Hook Form and Zod.

This guide will cover building forms using the Field component, adding schema validation with Zod, handling errors, ensuring accessibility, and more.

Demo

We’ll build a form with a text input and textarea. When you submit, the form data is validated and any errors will be shown.

Browser validation disabled

For the purposes of this demo, browser validation is disabled to illustrate schema validation. In production, keep native validation enabled when appropriate.

Bug Report
Help us improve by reporting bugs you encounter.
0/100 characters

Include steps to reproduce, expected behavior, and what actually happened.

Approach

This form uses React Hook Form for state and Zod for validation. We'll build forms using the Field component, which gives you complete flexibility over the markup and styling.

  • Uses React Hook Form's useForm hook for form state management.
  • Uses the Controller component for controlled inputs.
  • Uses the Field components for building accessible forms.
  • Uses client-side validation by passing your Zod schema into resolver.

Anatomy

Typical structure: wrap each field with Controller, and the Field component.

Form

Create a schema

Define your form shape with a Zod schema.

Note: React Hook Form supports other Standard Schema libraries; Zod is used here for clarity.

form.tsx

Setup

Create the form with useForm from React Hook Form and pass your schema to the resolver option.

form.tsx

Build

Build the form using the Controller from React Hook Form and the Shark Field.

Done

That's it. You now have a fully accessible form with client-side validation.

When you submit the form, the onSubmit function will be called with the validated form data. If the form is invalid, React Hook Form will display the errors on field.state.error for FieldError.

Validation

Client-side

React Hook Form validates your form data using the Zod schema. Define a schema and pass it to the resolver option of the useForm hook.

example-form.tsx

Modes

Configure when validation runs via the mode option:

form.tsx

Displaying Errors

Display errors next to the field using FieldError. For styling and accessibility:

  • Add the invalid prop to the Field component.
  • Don't need to add the invalid prop to the form control such as Input, Checkbox, etc.
form.tsx

Different types of fields

Input

  • Spread the field object onto the Input component.
  • Add the invalid prop to the Field component and pass the error message to the FieldError component.
Profile Settings
Update your profile information below.

This is your public display name. Must be between 3 and 10 characters. Must only contain letters, numbers, and underscores.

Textarea

  • Spread the field object onto the Textarea component.
  • Add the invalid prop to the Field component and pass the error message to the FieldError component.
Personalization
Customize your experience by telling us more about yourself.

Tell us more about yourself. This will be used to help us personalize your experience.

NativeSelect

  • Spread the field object onto the NativeSelect component.
  • Add the invalid prop to the Field component and pass the error message to the FieldError component.
Language Preferences
Select your preferred spoken language.

For best results, select the language you speak.

Select

  • Wire field.value and field.onChange to Select.
  • Add the invalid prop to the Field component and pass the error message to the FieldError component.
Language Preferences
Select your preferred spoken language.

For best results, select the language you speak.

Checkbox

  • Wire field.value and field.onChange to Checkbox.
  • Add the invalid prop to the Field component and pass the error message to the FieldError component.
  • Remember to add data-slot="checkbox-group" to the FieldGroup component for proper styling and spacing.
Notifications
Manage your notification preferences.
Responses

Get notified for requests that take time, like research or image generation.

Tasks

Get notified when tasks you've created have updates.

Radio group

  • Wire field.value and field.onChange to RadioGroup.
  • Add the invalid prop to the Field component and pass the error message to the FieldError component.
Subscription Plan
See pricing and features for each plan.
Plan

You can upgrade or downgrade your plan at any time.

Switch

  • Wire field.value and field.onChange to Switch.
  • Add the invalid prop to the Field component and pass the error message to the FieldError component.
Security Settings
Manage your account security preferences.

Enable multi-factor authentication to secure your account.

NumberInput

  • Wire field.value and field.onChange to NumberInput.
  • Add the invalid prop to the Field component and pass the error message to the FieldError component.
Salary expectations
Share your target gross annual salary in EUR.
EUR

Slider

  • Wire field.value and field.onChange to Slider.
  • Add the invalid prop to the Field component and pass the error message to the FieldError component.
Listings filter
Set a price range so we only show results in your budget.

Drag each thumb to set the lower and upper bound.

Combobox

  • Wire field.value and field.onChange to Combobox.
  • Add the invalid prop to the Field component and pass the error message to the FieldError component.
Role setup
Tell us which team you work with most so we can tailor workflows.

Type to filter the list, then pick one option.

Autocomplete

  • Wire field.value and field.onChange to Autocomplete.
  • Add the invalid prop to the Field component and pass the error message to the FieldError component.
Tech stack
Select the technology you're most familiar with.

Type to filter the list, then pick one option.

Date Picker

  • Wire field.value and field.onChange to DatePicker.
  • Add the invalid prop to the Field component and pass the error message to the FieldError component.
Scheduling
Pick a day that works for a first-round interview. We will confirm by email.

Input OTP

  • Wire field.value and field.onChange to InputOTP.
  • Add the invalid prop to the Field component and pass the error message to the FieldError component.
Account recovery
Enter one of your single-use backup codes if you cannot access your authenticator app.

Codes can only be used once.

Rating

  • Wire field.value and field.onChange to Rating.
  • Add the invalid prop to the Field component and pass the error message to the FieldError component.
Quick feedback
Help us improve by sharing how likely you are to recommend Shark UI to a colleague.

1 = not likely, 5 = very likely.

File Upload

  • Wire field.value and field.onChange to FileUpload.
  • Add the invalid prop to the Field component and pass the error message to the FieldError component.
Application materials
Upload your résumé or CV.

Accepted formats: PDF and Word documents.

Complex Forms

Here is an example of a more complex form with multiple fields and validation.

You're almost there!
Choose your subscription plan and billing period.
Subscription plan

Choose your subscription plan.

Choose how often you want to be billed.

Add-ons

Select additional features you'd like to include.

Advanced analytics and reporting

Automated daily backups

24/7 premium customer support

Receive email updates about your subscription.

Resetting the Form

Use form.reset() to reset the form to its default values.

Array Fields

React Hook Form provides a useFieldArray hook for managing dynamic array fields. This is useful when you need to add or remove fields dynamically.

Contact emails
Manage your contact email addresses.
Email addresses

Add up to 5 email addresses where we can contact you.

Using useFieldArray

Use the useFieldArray hook to manage array fields. It provides fields, append, and remove methods.

form.tsx

Array Field Structure

Wrap your array fields in a FieldSet with a FieldLegend and FieldDescription.

form.tsx

Controller Pattern for Array Items

Map over the fields array and use Controller for each item. Make sure to use field.id as the key.

form.tsx

Adding Items

Use the append method to add new items to the array.

form.tsx

Removing Items

Use the remove method to remove items from the array. Add the remove button conditionally.

form.tsx

Array Validation

Use Zod's array method to validate array fields.

form.tsx