Created
May 13, 2021 07:19
-
-
Save adarshaacharya/30e212b9f1ca711e26a61bbdb7feebf6 to your computer and use it in GitHub Desktop.
react-hook-form v6 simple example
This file contains 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 { useState } from 'react'; | |
import { SubmitHandler, useForm } from 'react-hook-form'; | |
import { BrowserRouter, Routes, Route } from 'react-router-dom'; | |
import { isLoggedInVar } from '../apollo'; | |
type Inputs = { | |
email: string; | |
password: string; | |
}; | |
export const LoggedOutRouter = () => { | |
const { | |
register, | |
watch, | |
handleSubmit, | |
formState: { errors }, | |
} = useForm<Inputs>(); | |
const onSubmit: SubmitHandler<Inputs> = (data) => { | |
console.log(data); | |
// isLoggedInVar(true); // updating reactive var | |
}; | |
console.log(watch()); | |
return ( | |
<div> | |
<h1>LOGIN PAGE</h1> | |
<form onSubmit={handleSubmit(onSubmit)}> | |
<div> | |
<input | |
{...register('email', { required: 'Email is required' })} | |
id="email" | |
/> | |
{errors.email && ( | |
<span className="text-red-500">{errors.email.message}</span> | |
)} | |
</div> | |
<div> | |
<input | |
{...register('password', { | |
required: 'Password is required', | |
minLength: { | |
value: 4, | |
message: 'Password should be at least 4 chars long.', | |
}, | |
})} | |
id="password" | |
/> | |
{errors.password && ( | |
<span className="text-red-500">{errors.password.message}</span> | |
)} | |
</div> | |
<button className="bg-yellow-300 text-white">Log In</button> | |
</form> | |
</div> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment