Created
October 14, 2019 14:09
-
-
Save riccardogiorato/cac57c9351307f913502419d8e27529b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import useForm from 'react-hook-form' | |
export default function App() { | |
const { register, handleSubmit, watch, errors } = useForm() | |
const onSubmit = data => { console.log(data) } | |
console.log(watch('example')) // watch input value by passing the name of it | |
return ( | |
{/* "handleSubmit" will validate your inputs before invoking "onSubmit" */} | |
<form onSubmit={handleSubmit(onSubmit)}> | |
{/* register your input into the hook by invoking the "register" function */} | |
<input name="example" defaultValue="test" ref={register} /> | |
{/* include validation with required or other standard HTML validation rules */} | |
<input name="exampleRequired" ref={register({ required: true })} /> | |
{/* errors will return when field validation fails */} | |
{errors.exampleRequired && <span>This field is required</span>} | |
<input type="submit" /> | |
</form> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment