form hook react code example

Example 1: install react hook form

npm install react-hook-form

Example 2: react hooks form

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

function App() {
  const { register, handleSubmit, errors } = useForm(); // initialize the hook
  const onSubmit = (data) => {
    console.log(data);
  };

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <input name="firstname" ref={register} /> {/* register an input */}
      <input name="lastname" ref={register({ required: true })} />
      {errors.lastname && 'Last name is required.'}
      <input name="age" ref={register({ pattern: /\d+/ })} />
      {errors.age && 'Please enter number for age.'}
      <input type="submit" />
    </form>
  );
}

Example 3: react hook form

import React from 'react';
import { useForm } from 'react-hook-form';
import "./App.css";

type Profile = {
  firstname: string
  lastname: string
  age: number
}

function App() {
  const {register, handleSubmit, errors} = useForm<Profile>()

  const onSubmit = handleSubmit((data) => {
    alert(JSON.stringify(data))
  })

  return (
    <main>
    <form onSubmit={onSubmit}>
      <div>
        <label htmlFor="firstname">First Name</label>
        <input ref={register({ required: true })} id="firstname" name="firstname" type="text"/>
        {
          errors.firstname && <div className="error">Enter your name</div>
        }
      </div>
      <div>
        <label htmlFor="lastname">Last Name</label>
        <input ref={register({ required: true })} id="lastname" name="lastname" type="text"/>
        {
          errors.lastname && <div className="error">Enter your last name</div>
        }
      </div>
      <div>
        <label htmlFor="age">Age</label>
        <input ref={register({ required: true })} id="age" name="age" type="text"/>
        {
          errors.age && <div className="error">Enter your age</div>
        }
      </div>
      <button type="submit">Save</button>
    </form>
    </main>
  );
}

export default App;

Example 4: React Hook Form

import React from 'react';
import ReactDOM from 'react-dom';
import { connect } from "react-redux";
import useForm from 'react-hook-form';

function SampleForm() {
  const { register, handleSubmit } = useForm();
  const onSubmit = data => {
    alert(JSON.stringify(data));
  };

  return (
    <div className="App">
      <form onSubmit={handleSubmit(onSubmit)}>
        <div>
          <label htmlFor="firstName">First Name</label>
          <input name="firstName" placeholder="bill" ref={register} />
        </div>

        <div>
          <label htmlFor="lastName">Last Name</label>
          <input name="lastName" placeholder="luo" ref={register} />
        </div>

        <div>
          <label htmlFor="email">Email</label>
          <input name="email" placeholder="[email protected]" type="email" ref={register} />
        </div>
        <button type="submit">Submit</button>
      </form>
    </div>
  );
}

class Sample extends Component {
  constructor(props) {
    super(props);
  }

  render() {
    return <SampleForm />;
  }
}

export default connect()(Sample);

const rootElement = document.getElementById('root');
ReactDOM.render(<Sample />, rootElement);

Example 5: react hook form

react hook form