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

183
Visualizações
React input loses focus on onChange

I'm making a React application and I need a table where each row contains an input. For that, I'm using Material-UI and Formik, but I have a bug that anytime I type something in one of the inputs, I lose the focus of that input. I saw this bug a lot on the internet, I tried with adding keys, deleting keys, use defaultValue instead of value, and anything worked properly for me.

I've created a codesandbox for this: https://codesandbox.io/s/gracious-robinson-i69v3?file=/src/App.js

If anyone could help me I would really appreciate, I'm stuck with this problems for a few days.

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

0

Everytime your MyInput components rerenders, it key changes because it's non deterministic (it uses Math.random()). Because of this, React will remove the existing DOM node and insert a new DOM node in it's place. Your browser now thinks it's an other element, so it does not put focus on that element.

about 4 years ago · Juan Pablo Isaza Relatório

0

You don't have to set the key property on every component. It's just needed where you output an array of elements, e.g. when using map. So in your example only the Formik component needs to have the key property set, because it's the parent of the other components inside the loop.

     {companies.map((company, index) => (
          <Formik
            key={index}
            initialValues={{
              contact_name: company.contact_name || "",
              contact_email: company.contact_email || ""
            }}
            onSubmit={(values) => {
              console.log(values);
            }}
          >
            {(formik) => (
...
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