Design-loving front-end engineer
Ryong
Design-loving front-end engineer
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
    • Framework
    • React
      • Concept
      • Library
      • Hook
      • Component
      • Test
    • NodeJS
    • Android
      • Concept
      • Code
      • Sunflower
      • Etc
    • Flutter
      • Concept
      • Package
    • Web
    • Web
    • CSS
    • Language
    • JavaScript
    • TypeScript
    • Kotlin
    • Dart
    • Algorithm
    • Data Structure
    • Programmers
    • Management
    • Git
    • Editor
    • VSCode
    • Knowledge
    • Voice
Design-loving front-end engineer

Ryong

React/Library

[ React ] react-hook-form

2022. 8. 4. 15:07

๐Ÿ”ด  Introduction

โšช  form์˜ validation์„ ๋„์™€์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

 

๐ŸŸ   Official

 

์‹œ์ž‘ํ•˜๊ธฐ

Performant, flexible and extensible forms with easy-to-use validation.

react-hook-form.com

 

๐ŸŸก  Install

 
npm i react-hook-form

 

๐ŸŸข  Usage

 
import "./styles.css";
import { useForm } from "react-hook-form";

export default function App() {
  const {
    register, // input์— react-hook-form ๋“ฑ๋ก
    handleSubmit, // register์— ๋“ฑ๋ก๋œ input ๋ฐ์ดํ„ฐ๋ฅผ form์—์„œ ์ฒ˜๋ฆฌํ•  ํ•จ์ˆ˜
    formState: { errors }, // ๊ฐœ๋ณ„ input์— ๋Œ€ํ•œ errors ๋ฐ˜ํ™˜
    setError // ์ปค์Šคํ…€ ์œ ํšจ์„ฑ์— ๋Œ€ํ•œ Error๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Œ
  } = useForm({
    defaultValues: {
      // input์— ๊ธฐ๋ณธ๊ฐ’ ์ž…๋ ฅ
      email: "@naver.com"
    }
  });

  const onValid = (data) => {
    console.log(data);
    // input์˜ data๋“ค์ด ์ธ์ž๋กœ ์ฃผ์–ด์ง
    if (data.password !== data.passConfirm) {
      setError(
        "passConfirm", // ๊ฐœ๋ณ„ ์š”์†Œ์— ์—๋Ÿฌ๋ฅผ ์ „๋‹ฌํ•ด์คŒ
        { message: "Password are not the same" }, // ์—๋Ÿฌ ๋ฉ”์‹œ์ง€
        { shouldFocus: true } // ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ input์— focus ์ ์šฉ
      );
    }
  };

  const onError = (error) => {
    console.log(error);
  };

  return (
    <div className="App">
      <form
        style={{ display: "flex", flexDirection: "column" }}
        onSubmit={handleSubmit(onValid, onError)}
      >
        {/* ๐Ÿ“ Form 1 : Email */}
        <input
          {...register("email", {
            required: "Email is required", // false ์‹œ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€
            pattern: {
              // ์ •๊ทœ์‹ ํ‘œํ˜„์„ ๋„ฃ์„ ์ˆ˜๋„ ์žˆ๋‹ค.
              value: /^[A-Za-z0-9._%+-]+@naver.com$/,
              message: "Only naver.com emails allowed"
            }
          })}
          placeholder="Email"
        />
        <span>{errors?.email?.message}</span>

        {/* ๐Ÿ“ Form 2 : firstName */}
        <input
          {...register("firstName", {
            required: "write here",
            validate: {
              // ์ปค์Šคํ…€ ๊ฐœ๋ณ„ ์œ ํšจ์„ฑ ํ•จ์ˆ˜ ์ ์šฉ ๊ฐ€๋Šฅ
              noRyong: (value) =>
                value.includes("ryong") ? "no ryong allowed" : true
            }
          })}
          placeholder="First Name"
        />
        <span>{errors?.firstName?.message}</span>

        {/* ๐Ÿ“ Form 3 : lastName */}
        <input
          {...register("lastName", { required: "write here" })}
          placeholder="Last Name"
        />
        <span>{errors?.lastName?.message}</span>

        {/* ๐Ÿ“ Form 4 : password */}
        <input
          {...register("password", { required: "write here", minLength: 5 })}
          placeholder="Password"
        />
        <span>{errors?.password?.message}</span>

        {/* ๐Ÿ“ Form 5 : passConfirm */}
        <input
          {...register("passConfirm", {
            required: "write here",
            minLength: {
              value: 5,
              message: "Your password is too short."
            }
          })}
          placeholder="passConfirm"
        />
        <span>{errors?.passConfirm?.message}</span>

        <button>Add</button>
        <span>{errors?.extraError?.message}</span>
      </form>
    </div>
  );
}

/* react-hook-form์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ๋•Œ์˜ ์ฝ”๋“œ
const App = () => {
  const [toDo, setToDo] = useState("");
  const [toDoError, setToDoError] = useState("");
  const onChange = (event) => {
    const {
      currentTarget: {value},
    } = event;
    setToDoError("");
    setToDo(value);
  };
  const onSubmit = (event) => {
    event.preventDefault();
    if (toDo.length < 10) {
      return setToDoError("To do should be longer");
    }
    console.log("submit");
  };
  return (
    <div>
      <form onSubmit={onSubmit}>
        <input onChange={onChange} value={toDo} placeholder="Write a to do" />
        <button>Add</button>
        {toDoError !== "" ? toDoError : null}
      </form>
    </div>
  )
}
*/

 

 

 

์ €์ž‘์žํ‘œ์‹œ (์ƒˆ์ฐฝ์—ด๋ฆผ)

'React > Library' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[ React ] framer-motion  (0) 2022.08.09
[ React ] react-beautiful-dnd  (0) 2022.08.08
[ React ] classnames  (0) 2022.08.04
[ React ] redux  (0) 2022.08.02
[ React ] react-router-dom  (0) 2022.07.31
    'React/Library' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • [ React ] framer-motion
    • [ React ] react-beautiful-dnd
    • [ React ] classnames
    • [ React ] redux
    Design-loving front-end engineer
    Design-loving front-end engineer
    ๋””์ž์ธ์— ๊ด€์‹ฌ์ด ๋งŽ์€ ๋ชจ๋ฐ”์ผ ์•ฑ ์—”์ง€๋‹ˆ์–ด Ryong์ž…๋‹ˆ๋‹ค.

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”