• Home
  • Jobs
  • Courses
  • Questions
  • Teachers
  • For business
  • ES/EN

0

16
Views
Handling value state with multiple Formik forms on one component

I am building a page using React and Formik. On my form page, I have three Formik forms that are conditionally rendered using a switch statement. I am doing it this way because each form's data need to be sent to their own api endpoint so I'm keeping them logically separated. When the first form is submitted, it cycles to the next. I am having an issue where when accessing the value prop in the second Formik form, it displays the values from the first. It appears as though Formik by default isn't handling the data separation when multiple Formik's exist, despite them existing in different return statements.

My switch statement is something like this:

switch(currentform) {
    case 'form1':
        return (
            <Formik
                initialValues={form1Values}
                onSubmit={(values, actions) => {
                    let form1= JSON.stringify(values, null, 2);
                    console.log(form1)
                 }}
                 >
                {({ values, setFieldValue }) => (
                    <Form className='form-container'>
                    
                 </Form>
                 )}
                </Formik>

        )
     case 'form2':
        return (
            <Formik
                initialValues={form2Values}
                onSubmit={(values, actions) => {
                    let form2= JSON.stringify(values, null, 2);
                    console.log(form2)
                 }}
                 >
                {({ values, setFieldValue }) => (
                    <Form className='form-container'>
                    
                 </Form>
                 )}
                </Formik>

        )
}

When submitting form1, the console.log(form1) displays the correct data. After submitting form2, the console.log(form2) returns the same data as form1. I've tried renaming the values prop in each onSubmit but that doesn't seem to change the behavior. Can someone help to confirm if my implementation is flawed or if there is a better way to configure this? Thanks to anyone who reads this!

about 1 month ago ·

Juan Pablo Isaza

Answer question
Find remote jobs
Loading

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2022 PeakU Inc. All Rights Reserved.