이번 프로젝트 결제 페이지에서 사용하게 된 useForm
npm install react-hook-form
import { useForm } from "react-hook-form";
import { ErrorMessage } from "@hookform/error-message";
const { register, errors, handleSubmit } = useForm();
const onSubmit = (data) => {
console.log("data", data);
console.log("m", ErrorMessage);
};
const { register, errors } = useForm();
<InfoBox>
{USERBILL.map((userData, idx) => {
return (
<UserBillBox key={idx}>
<label>{userData.title}</label>
<input
type={userData.type}
name={userData.name}
ref={register({ required: true })}
/>
<ErrorMessage
errors={errors}
name={userData.name}
message={userData.message}
as="p"
/>
</UserBillBox>
);
})}
</InfoBox>
const USERBILL = [
{
id: 1,
title: "Country / Region *",
name: "country",
type: "text",
required: true,
message: "Country / Region field is required.",
},
...
]
export default USERBILL