React Form Validation Using React Hooks

Vince Llauderes
HackerNoon.com
6 min readJul 7, 2019

--

Photo by Efe Kurnaz on Unsplash

React 16.8 updates introduce a new feature in their library called Hooks. Hooks is one of the most revolutionary updates happened in React library. It gives you a lot more flexibility in writing stateful components w/out writing classes. There’s a saying “Write less, Do more” you’ll avoid much boilerplate code in your React application.

So in this article I’m going to share with you on how I created my own form validation using React hooks.

Before we get started, the complete repository can be downloaded in my GitHub profile. Feel free to modify the code if you have something to share or comments on it Enjoy!

Okay so first let us create a file called useForm.js in this file we should place our form validation hooks for separation of concerns. We follow the convention in React library that every functional component that uses hooks starts with “use” keyword followed by their purpose, to easily determine what they can do.

In this file, let’s import first all the hooks that we needed in React library.

After that let’s create a function called useForm and has 3 parameters.

The first parameter in our useForm function is stateSchema. In this parameter, we should define our state in our form.

Example:

Our stateSchema object consist all the fields in our form. As you can see here we have an fname and has object value containing the property of value and error. The value is the value on what we type in our input field and the error is the error in our input field if there’s any.

The second parameter is validationSchema. In this parameter we define our own rules in validating our state in our form.

Example:

As you’ve noticed here stateSchema property and validationSchema property is the same. Because we need to map the validation in stateSchema to validate fields that we define in stateSchema (eg: fname, lname and tags etc).

The third parameter is callback function that we execute if we submit the form. Usually we placed here the api for handling form submission.

Our useForm function returns values such as (state, disable, handleOnChange, handleOnSubmit)

state returns an object of current state in our form.

disable returns a boolean to be able to disable the submit button if there’s an error in the state

handleOnChange returns a function to set the values in our form state.

handleOnSubmit returns a function to submit the values in our form.

So we’re now done creating our useForm hooks and we can now hook it in our Component. Before that let’s create first our Form component.

In our Form component we used our previously created hooks called useForm and passed all the required argument to be able to use it.

Our stateSchema:

Our validationSchema:

Our callback function in submitting form:

Passed all the required argument in useForm to use it.

Remember our useForm hooks returns a values such as state, handleOnChange, handleOnSubmit, disable.

With that we can now use it in our Form component.

After that let us import our Form component in App.js.

Here’s the final output of our created useForm hooks with Form component.

using useForm hooks in Form component

Hope it helps 😃

If you enjoy reading this article give me a clapped 👏 👏 👏

Thank you.

“Don’t be a JavaScript Mediocre.”

Follow me on twitter https://twitter.com/llaudevc/

--

--