https://light9639.github.io/React-Hook-Form/
β¨ π οΈ React-Hook-Form μ°μ΅ νμ΄μ§μ λλ€. β¨
- 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μ λ€μ λͺ λ Ήμ΄λ‘ μ€μΉνλ€.
npm install react-hook-form styled-components yup @hookform/resolvers
# or
yarn add react-hook-form styled-components yup, @hookform/resolversreact-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>
)
}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μ κ°μ μ λ ₯νλ©΄ μ½μμ ν΅ν΄ κ°μ νμΈν μ μλ€.

