React Hook Form and Zod match made in heaven. React Hook Form is a library that helps you to manage forms in React. Zod is a library that helps you to validate data in JavaScript. In this blog, I will show you how to use Zod with React Hook Form.
Why use React Hook Form? Why can't I use normal form handling in React?
Handling Forms in react is a bit complex, you have to manage the state of the form, validate the form, and then submit the form. React Hook Form simplifies this process by providing a simple API to manage forms.
While you are managing form states, whenever a person types his data the states changes and the component re-renders, this can be a performance issue. React Hook Form solves this by using uncontrolled components
.
Why use Zod? Why can't I use normal validation in React?
As form size grows its very hard to validate data manually, Zod provides a simple API to validate data. It also provides a way to validate nested data and provides a way to validate data asynchronously.
But I'm using Typescript, why can't I use Typescript to validate data?
Typescript is a compile-time type checker, it can't validate data at runtime. Zod provides a way to validate data at runtime. In simple words Typescript can validate data types written in code but Zod can validate data generated while running the code, like user input etc.
Setup
First, you need to install React Hook Form and Zod.
Usage
I'll be using Typescript in this blog, you can use Javascript as well.
Form without using Zod and React Hook Form
See in the above example we are maintaining so many states and checking for errors manually. This can be a bit complex when the form size grows. For smaller forms this is a good approach but for larger forms managing so many states and the performance issue can be a problem.
Now let's see how we can use React Hook Form and Zod to simplify this process.
And thats it, a simple validation using Zod and React Hook Form. This is a simple example, you can use Zod to validate nested data, validate data asynchronously, and much more.
If you are using Typescript and want to infer the types from schema you can use
Steps
Lets look at the steps once again.
- Import
useForm
fromreact-hook-form
,zodResolver
from@hookform/resolvers/zod
, andz
fromzod
.
- Create a schema using Zod.
- Use
useForm
and pass the schema to theresolver
key.
- Use
register
to register the input fields.
- Use
errors
to show the error message.
- Use
handleSubmit
to submit the form.
That was simple.
Handling server errors
If you are sending data to the server and server throws an error you can handle it with setError
function to show the error message.
Say you have a global error message that you want to show when the server returns an error you can use the setError
function with root as the name.
and show the error message like this
Handling submitting state
If you want to show a loading spinner when the form is submitting you can use the isSubmitting
property from formState
.
And show the spinner or disable the submit button like this.
Showing custom error messages
If you want to show custom error messages you can use the message
key in the schema.
Conclusion
React Hook Form and Zod can make your forms robust and easy to manage. This was a simple tutorial on how to use Zod with React Hook From. There are tons of features in React Hook Form and I would recommend you to read more about Zod and React Hook Form to explore more such amazing features.
Bye Bye 👋