Skip to content

Instantly share code, notes, and snippets.

@iamhaiderkhan
Created April 4, 2023 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamhaiderkhan/c212e6660592a42efdba9bdd03981965 to your computer and use it in GitHub Desktop.
Save iamhaiderkhan/c212e6660592a42efdba9bdd03981965 to your computer and use it in GitHub Desktop.
import React from "react";
import { useForm } from "react-hook-form";
function SignUpComponent() {
const { register, handleSubmit, errors } = useForm();
const onSubmit = (data) => {
// logs {firstName:"exampleFirstName", lastName:"exampleLastName"}
console.log(data);
};
return (
<form onSubmit={handleSubmit(onSubmit)}>
<input name="firstName" ref={register} />
<input name="lastName" ref={register({ required: true })} />
{errors.lastName && <span>"Last name is a mandatory field."</span>}
<input name="age" ref={register({ required: true })} />
{errors.age && <span>"Please enter number for age."</span>}
<input type="submit" />
</form>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment