Posts react javascript hook form
Post
Cancel

react javascript hook form

create react

1
2
npx create-react-app react_javascript_example
yarn add react-hook-form 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import React from "react";
import { useForm } from "react-hook-form";

function App() {
  const {
    register,
    handleSubmit,
    formState: { errors },
  } = useForm();

  const onSubmit = (data) => {
    console.log(data);
  };

  return (
    <form onSubmit={handleSubmit(onSubmit)} style=>
      <input
        type="text"
        placeholder="email"
        name="email"
        {...register("email", { required: true })}
      />
      <input
        type="text"
        placeholder="password"
        name="password"
        {...register("password", { required: true })}
      />
      <input type="submit" />
        {errors.email && <div role="alert">please write email</div>}
        {errors.password && <div role="alert">please write password</div>}
    </form>
  );
}

export default App;

결과

This post is licensed under CC BY 4.0 by the author.