Skip to content

Instantly share code, notes, and snippets.

@fivethreeo
Last active September 1, 2021 13:45
Show Gist options
  • Save fivethreeo/8089dc7658dcebb6293fd93707b677f7 to your computer and use it in GitHub Desktop.
Save fivethreeo/8089dc7658dcebb6293fd93707b677f7 to your computer and use it in GitHub Desktop.
formikexample.js
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