Skip to content

light9639/React-Hook-Form

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

⚑ React-Hook-Form μ—°μŠ΅ νŽ˜μ΄μ§€μž…λ‹ˆλ‹€.

:octocat: https://light9639.github.io/React-Hook-Form/

127 0 0 1_5173_

✨ πŸ› οΈ React-Hook-Form μ—°μŠ΅ νŽ˜μ΄μ§€μž…λ‹ˆλ‹€. ✨

πŸŽ‰ React 생성

  • React 생성
npm create-react-app my-app
# or
yarn create react-app my-app
  • viteλ₯Ό μ΄μš©ν•˜μ—¬ ν”„λ‘œμ νŠΈλ₯Ό μƒμ„±ν•˜λ €λ©΄
npm create vite@latest
# or
yarn create vite
  • ν„°λ―Έλ„μ—μ„œ μ‹€ν–‰ ν›„ ν”„λ‘œμ νŠΈ 이름 λ§Œλ“  ν›„ React 선택, Typescirpt μ„ νƒν•˜λ©΄ 생성 μ™„λ£Œ.

πŸ›Έ react-hook-form, styled-components, yup, @hookform/resolvers μ„€μΉ˜

  • react-hook-form, styled-components, yup, @hookform/resolvers을 λ‹€μŒ λͺ…λ Ήμ–΄λ‘œ μ„€μΉ˜ν•œλ‹€.
npm install react-hook-form styled-components yup @hookform/resolvers
# or
yarn add react-hook-form styled-components yup, @hookform/resolvers

βœ’οΈ App.tsx, Style.ts μˆ˜μ • 및 μž‘μ„±

⚑ App.tsx

  • react-hook-form을 importν•˜μ—¬, Style.ts에 μž‘μ„±λ˜μ–΄μžˆλŠ” styled-components μž‘μ„± 자료 κ°€μ Έμ˜¨λ‹€.
  • yup 라이브러리λ₯Ό μ΄μš©ν•˜μ—¬ schemaλ₯Ό μ œμž‘ν•˜κ³  useForm에 데이터λ₯Ό λŒ€μž…ν•œλ‹€.
import reactLogo from './assets/react.svg'
import FormImg from './assets/form.svg'
import { useForm } from "react-hook-form";
import './App.css'
import * as S from './Style';
import { yupResolver } from "@hookform/resolvers/yup";
import * as yup from "yup";

export default function App(): JSX.Element {
  const schema = yup.object().shape({
    fullName: yup.string().required("Your Full Name is Required!"),
    email: yup.string().email().required(),
    age: yup.number().positive().integer().min(18).required(),
    password: yup.string().min(4).max(20).required(),
    confirmPassword: yup
      .string()
      .oneOf([yup.ref("password")], "Passwords Don't Match")
      .required(),
  });

  const {
    register,
    handleSubmit,
    formState: { errors },
  }: any = useForm({
    resolver: yupResolver(schema),
  });

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

  return (
    <div className="App">
      <div className='ImgBox'>
        <a href="https://react-hook-form.com/" target="_blank">
          <img src={FormImg} className="logo" alt="form logo" />
        </a>
        <a href="https://reactjs.org" target="_blank">
          <img src={reactLogo} className="logo react" alt="React logo" />
        </a>
      </div>
      <h1>React Hooks Form</h1>
      <div className="card">
        <S.Form onSubmit={handleSubmit(onSubmit)}>
          <S.Input type="text" placeholder="Full Name..." {...register("fullName")} />
          <S.Text>{errors.fullName?.message}</S.Text>
          <S.Input type="text" placeholder="Email..." {...register("email")} />
          <S.Text>{errors.email?.message}</S.Text>
          <S.Input type="number" placeholder="Age..." {...register("age")} />
          <S.Text>{errors.age?.message}</S.Text>
          <S.Input
            type="password"
            placeholder="Password..."
            {...register("password")}
          />
          <S.Text>{errors.password?.message}</S.Text>
          <S.Input
            type="password"
            placeholder="Confirm Password..."
            {...register("confirmPassword")}
          />
          <S.Text>{errors.confirmPassword?.message}</S.Text>
          <S.Submit type="submit" />
        </S.Form>
        <p>
          Edit <code>src/App.tsx</code> and save to test HMR
        </p>
      </div>
      <p className="read-the-docs">
        Click on the Form and React logos to learn more
      </p>
    </div>
  )
}

⚑ Style.ts

  • styled-componentsλ₯Ό importν•œ λ’€ 각각의 μŠ€νƒ€μΌμ„ export ν•œ λ‹€μŒ App.tsxμ—μ„œ μ‚¬μš©ν•˜μ—¬ μŠ€νƒ€μΌλ§ν•œλ‹€.
import styled from 'styled-components';

export const Form = styled.form`
    display: flex;
    flex-direction: column;
    justify-content: center;
    vertical-align: center;
    margin-bottom: 30px;
`

export const Input = styled.input`
    padding: 10px 5px;
    border-radius: 5px;
    font-size: 14px;
    width: 300px;
    border: 1px solid gray;
    margin: 0 auto;
`

export const Text = styled.p`
    font-size: 0.875rem;
    color: #ff0000;
    text-align: center;
`

export const Submit = styled.input`
    padding: 10px 5px;
    border-radius: 5px;
    font-size: 14px;
    width: 310px;
    border: none;
    background-color: #EC5990;
    color: white;
    margin: 0 auto;
`

πŸ§ͺ input λ²„νŠΌ 클릭 ν›„ μ½˜μ†” 확인

  • input에 값을 μž…λ ₯ν•˜λ©΄ μ½˜μ†”μ„ 톡해 값을 확인할 수 μžˆλ‹€.

Img

About

πŸ› οΈ React-Hook-Form μ—°μŠ΅ νŽ˜μ΄μ§€μž…λ‹ˆλ‹€.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published