I'm having trouble understanding Formik and how it manages state. I have multiple components in my app that have forms. These forms have a lot of overlap on purpose. When I get values in one component, I would like the other components to have access to those values too.
I have contemplated using useState or useReducer for each and everyone of the values but I've done some reading and found out that Formik manages state itself? So my problem here would be really just to access the values from 'initialValues' in a certain component in different components?
import { Formik, Field, Form, ErrorMessage } from 'formik';
import * as Yup from 'yup';
<Formik
initialValues={{ name: '', ... }} //how can I use these values in other components?
//how can I 'save' them so that if the user
//accesses another page with a form,
//the data that has already been provided is accessible?
onSubmit={(values, { setSubmitting }) => {
setTimeout(() => {
alert(JSON.stringify(values, null, 2));
setSubmitting(false);
}, 400)
}}
>
<Form>
<label htmlFor="name">Name</label>
<Field maxLength="51"
name="name"
type="text"
/>
<ErrorMessage name="name" />
...
</Form>
</Formik>