react-hook-form
์ ๋ ฅ ํผ์ ๊ฐ๋ฐํ๋ ๊ฒ์ ๊ฐ๋ฐ์ ํ๋ฉฐ ๋๋ถ๋ถ์ ๊ฒฝ์ฐ ์ ํ๊ฒ ๋๋ ํ์ ์ค ํ๋์ด๋ค. ์์ form ๊ฒ์๊ธ์์ ๋ค๋ค๋ฏ์ด, ๊ฐ ์ ๋ ฅ ํ๋์ ์ํ ๊ด๋ฆฌ, ์ํ ์ ํจ์ฑ ๊ฒ์ฌ, ์๋ฃ ๋ก์ง ๋ฑ ์ฝ๋๊ฐ ํ์ฐ์ ์ผ๋ก ๋ง์์ง๊ธฐ ๋๋ฌธ์ ๋ณต์กํด์ง๊ธฐ ์ฝ๋ค. ์ด ์ ๋ ฅํผ์ ๊ฐํธํ๊ฒ ๊ตฌํํ ์ ์๊ฒ ๋์์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ๋ฐ๋ก react-hook-form ์ด๋ค.
react-hook-form
Performant, flexible and extensible forms with easy-to-use validation.
npm install react-hook-form
yarn add react-hook-form
์์ ์ฝ๋๋ฅผ ํตํด ์ด๋ป๊ฒ ์๋ํ๋์ง ์ดํด๋ณด๋๋ก ํ์.
import { useForm } from "react-hook-form";
export default function App() {
const { register, handleSubmit, watch, formState: { errors } } = useForm();
const onSubmit = data => console.log(data);
console.log(watch("example"));
return (
<form onSubmit={handleSubmit(onSubmit)}>
<input defaultValue="test" {...register("example")} />
<input {...register("exampleRequired", { required: true })} />
{errors.exampleRequired && <span>This field is required</span>}
<input type="submit" />
</form>
);
}
๋จผ์ useForm
์ด๋ผ๋ ๋ชจ๋์ importํด ์ฌ๋ฌ ๋ฉ์๋ ๋ค์ ์ฌ์ฉํ๋๊ฒ์ ๋ณผ ์ ์๋ค.
register
๋ผ๋ ๋ฉ์๋๋ ๋ฌธ์์ด์ ์ธ์๋ก ๋ฐ๊ฒ ๋๋๋ฐ, ํด๋น ๋ฌธ์์ด์ ์ํ๊ฐ์ผ๋ก ๋ฑ๋กํด์ฃผ๋ ์ญํ ์ ํ๋ค.
handleSubmit
๋ ์ ์ถ์ ์คํ๋ ์ด๋ฒคํธ ํธ๋ค๋ฌ ํจ์๋ฅผ ์ธ์๋ก ๋ฐ๊ฒ ๋๋ค. ์ดํ, onSubmit์ ์์ฑ๊ฐ์ผ๋ก ์ฌ์ฉ๋๋ค.
watch๋ ๋จ์ด ๊ทธ๋๋ก ๋ด๊ฐ register๋ก ์ค์ ํ ์ํ๊ฐ๋ค์ ํ์ฌ ๊ฐ๋ค์ ๋ณด์ฌ์ค๋ค.
์ด๋ ๊ฒ ๊ธฐ์กด useState๋ก ๊ด๋ฆฌํ๋ ์ํ๊ฐ๋ค์ ๊ฐ๋จํ๊ณ ํจ์จ์ ์ผ๋ก ๋ค๋ฃฐ์ ์๊ฒ ๋์์ค๋ค. ๋ํ ์ํ๊ฐ์ ์ ํจ์ฑ ๊ฒ์ฌ ์ธก๋ฉด๋ ์ง์์ ํ๊ณ ์๋ค.
import { useForm } from "react-hook-form";
export default function App() {
const { register, handleSubmit } = useForm();
const onSubmit = data => console.log(data);
return (
<form onSubmit={handleSubmit(onSubmit)}>
<input {...register("firstName", { required: true, maxLength: 20 })} />
<input {...register("lastName", { pattern: /^[A-Za-z]+$/i })} />
<input type="number" {...register("age", { min: 18, max: 99 })} />
<input type="submit" />
</form>
);
}
inputํ๊ทธ์ ์ฌ์ฉ๋ register๋ฅผ ์ดํด๋ณด๋ฉด, ํ์์์ฑ์ฌ๋ถ, ์ต๋๊ธธ์ด, ํจํด ๋ฑ์ ์ง์ ํด ์ ํจ์ฑ ๊ฒ์ฌ๋ฅผ ์งํํ๋ ํจ์๋ฅผ ๋ง๋ค์ด ์ฌ์ฉํ๋ ์๊ณ ๋ฅผ ๋ ์ ์๋ค.
๊ทธ๋ฐ๋ฐ ๋ง์ฝ MUI ๊ฐ์ ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํด์ผ ํ๋ ์ํฉ์๋ ์์์ ์ธ๊ธํ ๋ฉ์๋๋ค์ ์ฌ์ฉํ ์ ์๋๊ฐ? ์ด๋ฌํ ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ ์์ฒด ์ํ๊ด๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ react-hook-form๊ณผ ๊ฒฐํฉ์ด ์ฝ์ง์๋ค.
react-hook-form์์๋ ์ด๋ฌํ ์ํฉ์ ์ํด Controller
๋ผ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค. ์์์ฝ๋๋ก ์ดํด๋ณด์.
import React from 'react';
import { useForm, Controller } from 'react-hook-form';
import { TextField } from '@mui/material';
type FormValues = {
username: string;
};
export default function App() {
const { control, handleSubmit } = useForm<FormValues>();
const onSubmit = (data: FormValues) => {
console.log(data);
};
return (
<form onSubmit={handleSubmit(onSubmit)}>
<Controller
name="username"
control={control}
defaultValue=""
render={({ field }) => (
<TextField
{...field}
label="Username"
variant="outlined"
fullWidth
/>
)}
/>
<button type="submit">Submit</button>
</form>
);
}
์ ์์ ์ฝ๋๋ MUI ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ๋ ์์์ด๋ค. TextArea๋ผ๋ ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด Controller ์ปดํฌ๋ํธ์ render ์์ฑ์ ๋๊ฒจ์ ์ฌ์ฉํ๋ค. ์ด๋ฌํ ๊ฒฝ์ฐ์ธ, ๋น๋๊ธฐ ๋ฐ์ดํฐ, ์ํ๋๊ธฐํ๊ฐ ํ์ํ ๊ฒฝ์ฐ์๋ Controller๋ฅผ ์ฌ์ฉํ๋ค.
Last updated