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

93
Visualizações
React Formik checkbox condition based value remove in other Fields

I am using react-formik for form I have one issue with checkbox based condition, the check relation to another two fields if user checked the hidden fields are showing else it's hidden, the user unchecked the remaining two contains values, but I want if user unchecked all fields to empty.

My code:

Edit ConditionCheck

Thanks for help

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

when you want to control the value of one field based on the value of other field you can make use of the setFieldValue provided by Formik.

The Field components has a render prop which is a function and which helps us to access the setFieldValue in its argument. Now with the setFieldValue being accessible you can use it set the value of the dependent fields when the checkbox is checked or unChecked.

You need to render the Field component for your checkbox as below

<Field
  name="airconditioning"
  render={({ meta, form }) => {
    return (
      <Checkbox
        checked={meta.value}
        onChange={(e) => {
          const { checked } = e.target;
          if (!checked) {
            form.setFieldValue("airconditioningType", "");
            form.setFieldValue("airconditioningStarred", "");
          }
          form.setFieldValue("airconditioning", checked || "");
        }}
        name="checkedB"
        color="primary"
      />
    );
  }}
/>

Working Sandbox

about 4 years ago · Juan Pablo Isaza Relatório
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