Hi everyone…
I’m having a this problem with “React hook library” type called “FieldValues
”.
When i {handleSubmit()}
data from within <form>
it shows no errors.
Please note that “data
” has type of “FieldValues
”
but when i store this logic in separate function “onSubmit
” it throws error for the type, even though it has type of “FieldValues
”.
Please let me know if i made some error. Thank you. Here is the code:
import { useForm } from "react-hook-form";
function Form() {
const { register, handleSubmit } = useForm();
const onSubmit = (data: FieldValues) => console.log(data);
return (
<form onSubmit={handleSubmit(onSubmit)}>
<div className="mb-3">
<label htmlFor="name" className="form-label">
Your Name
</label>
<input
{...register("name")}
id="name"
type="text"
className="form-control"
/>
</div>
<div className="mb-3">
<label htmlFor="age" className="form-label">
Your Age
</label>
<input
{...register("age")}
id="age"
type="number"
className="form-control"
/>
</div>
<button className="btn btn-primary" type="submit">
Submit
</button>
</form>
);
}
export default Form;