Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

177
Visualizações
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 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda