Last active
September 1, 2021 13:45
-
-
Save fivethreeo/8089dc7658dcebb6293fd93707b677f7 to your computer and use it in GitHub Desktop.
formikexample.js
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 BasicExample = () => { | |
const buttonRef = useRef(); | |
return ( | |
<div> | |
<h1>My Form</h1> | |
<Formik | |
initialValues={{ name: "jared" }} | |
onSubmit={(values, actions) => { | |
setTimeout(() => { | |
alert(JSON.stringify(values, null, 2)); | |
alert(buttonRef.current); | |
actions.setSubmitting(false); | |
}, 1000); | |
}} | |
> | |
{(props) => { | |
const submitOne = () => { | |
buttonRef.current = "one"; | |
props.submitForm(); | |
}; | |
return ( | |
<form onSubmit={props.handleSubmit}> | |
<input | |
type="text" | |
onChange={props.handleChange} | |
onBlur={props.handleBlur} | |
value={props.values.name} | |
name="name" | |
/> | |
{props.errors.name && ( | |
<div id="feedback">{props.errors.name}</div> | |
)} | |
<button type="button" onclick="submitOne()"> | |
Submit | |
</button> | |
</form> | |
); | |
}} | |
</Formik> | |
</div> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment