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

151
Visualizações
React - how to have controlled inputs with keys without loss of focus on input?

I understand that React allows you to use the key prop on a Component to simulate a controlled Component: https://kentcdodds.com/blog/understanding-reacts-key-prop

What I am wondering, however, is if there is a way to simulate this without the constant loss of focus on an <input> tag with using the defaultValue property and NOT the value or onChange event AND have the defaultValue property be dynamic? - I understand the loss of focus is triggered because of the re-rendering with the key change.

Example:

<input key={someKey} defaultValue={someDefaultValue} onBlur={onBlurHandler} />

The above example won't allow for dynamic updating of the defaultValue property in the event that someDefaultValue changes, from what I understand, because the key and the defaultValue properties are not the same.

However, this will work for a dynamic update of the defaultValue property:

<input key={someKey} defaultValue={someKey} onBlur={onBlurHandler} />

The problem with this, however, is the constant loss of focus if this value is dynamic.

I understand that defaultValue is an initial load property but the controlled input with keys is a way to circumvent that.

What I am asking is - is there a way to have that defaultValue property update after the initial load and NOT cause a loss of focus for the provided input component?

Thank you for your time.

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