Listing 11-21: Using useForm
import {useForm} from 'react-hook-form';
function SignUpForm() {
const {register, handleSubmit} = useForm();
const onSubmit = data => {
console.log(data);
};
return (
<form onSubmit = {handleSubmit(onSubmit)}>
<label>First Name: </label>
<input name="firstname" {...register("firstname",{required:true})} />
<label>Last Name: </label>
<input name="lastname" {...register("lastname",{required:true})} />
<input type="submit" />
</form>
);
}
export default SignUpForm;
Download the examples, report issues, and ask/answer questions in the discussion area by visiting the book's github page. All of the code for the book is also available on codesandbox.io for you to play around with.
ReactJS Foundations is published by John Wiley and Sons, Inc and is available in paperback and eBook.