Created
May 23, 2020 09:38
-
-
Save Gauthamjm007/82edec2a67ace287d810b6b0beb7e134 to your computer and use it in GitHub Desktop.
Formik Login Page 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 React from "react"; | |
import { Formik } from "formik"; | |
const Login = () => ( | |
<div> | |
<h3>Login</h3> | |
<Formik | |
initialValues={{ email: "", password: "" }} | |
validate={(values) => { | |
const errors = {}; | |
if (!values.email) { | |
errors.email = "Required"; | |
} else if ( | |
!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i.test(values.email) | |
) { | |
errors.email = "Invalid email address"; | |
} | |
return errors; | |
}} | |
onSubmit={(values, { setSubmitting }) => { | |
setTimeout(() => { | |
alert(JSON.stringify(values, null, 2)); | |
setSubmitting(false); | |
}, 400); | |
}} | |
> | |
{({ | |
values, | |
errors, | |
touched, | |
handleChange, | |
handleBlur, | |
handleSubmit, | |
isSubmitting, | |
}) => ( | |
<form onSubmit={handleSubmit}> | |
<input | |
type="email" | |
name="email" | |
onChange={handleChange} | |
onBlur={handleBlur} | |
value={values.email} | |
/> | |
{errors.email && touched.email && errors.email} | |
<input | |
type="password" | |
name="password" | |
onChange={handleChange} | |
onBlur={handleBlur} | |
value={values.password} | |
/> | |
{errors.password && touched.password && errors.password} | |
<button type="submit" disabled={isSubmitting}> | |
Submit | |
</button> | |
</form> | |
)} | |
</Formik> | |
</div> | |
); | |
export default Login; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment