๐ด 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 |