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

159
Visualizações
React hook form registering new fields with previous values

I'm using react-hook-forms to gather localised content, when I set the defaultValues field in the useForm method I might not have all the default values set. For example, it might look like this:

{
  en: {
    first_text: "English first text",
    second_text: "English second text"
  }
}

Note: Adding the default empty values for each field as an empty string for each locale isn't really an option here.

I have created a simplified code sandbox here: https://codesandbox.io/s/react-hook-form-with-multi-locale-pnx11?file=/src/Child.tsx that displays the 2 fields mentioned above and a language switcher.

When I switch the language by clicking the button the issue I encounter is that my input fields (and the formData) get updated with the previous data that was present in the field instead of showing an empty input field.

so:

{
  en: {
    first_text: "English first text",
    second_text: "English second text"
  }
}

becomes:

{
  en: {
    first_text: "English first text",
    second_text: "English second text"
  },
  fr: {
    first_text: "English first text", // instead of ""
    second_text: "English second text" // instead of ""
  }
}

From what I understand the form values get updated when the Controller components get re-rendered but I don't understand why and how I can prevent this. I couldn't find a way to make sure that if I call getValues with a path that doesn't lead to any default value then to render an empty string (or another default value).

Few things I've tried are to pass an empty string as the defaultValue of the Controller components and also to unregister the fields but no success there.

Any advice/help would be highly appreciated!

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