React Hook Form

Full Stack Developed by: Donald Boulton Follow Don on Twitter🙌 0
Apr 23, 20193 min readReact, Hooks, Validation  Category Tech

React Hook Forms

↩️ I chose this module to validate my forms, its lightweight and integrates with yup schemas. Plus a nice online form builder and right on point instructions.

You can see this form validation in action on my contact page and on my email form in a modali window, the button for the popup email form is in the email section at the lower part of every PubliusLogic page.

🔗 简体中文


🔗 React Hooks Form

React hook form validation without the hassle

npm downloads npm npm

  • Super easy to integrate and create forms
  • Built with React Hooks with performance and developer experience in mind
  • Follows HTML standard for validation
  • Tiny size without other any dependency
  • Build forms quickly with the 🔗 form builder


$ npm install react-hook-form

$ yarn add react-hook-form

For validation schemas use yup

$ npm install yup

$ yarn add yup

Yup at code sandbox



import React from "react";
import { useForm } from "react-hook-form";

export default function App() {
  const { register, handleSubmit, watch, formState: { errors } } = useForm();
  const onSubmit = data => console.log(data);

  console.log(watch("example")); // watch input value by passing the name of it

  return (
    /* "handleSubmit" will validate your inputs before invoking "onSubmit" */
    <form onSubmit={handleSubmit(onSubmit)}>
      {/* register your input into the hook by invoking the "register" function */}
      <input defaultValue="test" {...register("example")} />
      {/* include validation with required or other standard HTML validation rules */}
      <input {...register("exampleRequired", { required: true })} />
      {/* errors will return when field validation fails  */}
      {errors.exampleRequired && <span>This field is required</span>}
      <input type="submit" />


If you would like to centralize your validation rules or external validation schema, you can apply validationSchema when you invoke useForm. we use Yup for object schema validation and the example below demonstrate the usage.

import React from "react";
import { useForm } from "react-hook-form";
import { yupResolver } from '@hookform/resolvers/yup';
import * as yup from "yup";

const schema = yup.object().shape({
  firstName: yup.string().required(),
  age: yup.number().positive().integer().required(),

export default function App() {
  const { register, handleSubmit, formState:{ errors } } = useForm({
    resolver: yupResolver(schema)
  const onSubmit = data => console.log(data);

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <input {...register("firstName")} />
      <input {...register("age")} />
      <input type="submit" />


Thanks goes to these wonderful people:

🔗 [AyumiKai] 🔗 garthmcrae 🔗 [erikras]

Sooner Girls

National Champions For the Fifth Time, Go Sooners!

Css Tricks

Front-End UI, Javascript & Functions, HTML Tips, and More!

Fox News

The Truth In the Latest World and National News.