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

263
Visualizações
Cannot set properties of null in React

I have two elements that are supposed to appear depending on whether the condition is true or false but on one of them I need to use useRef hook but it doesn't work. Even though the element sometimes renders first, I still get the error Cannot set properties of null. I need to edit the second element's value when it renders with its reference but the code crashes so I can't do anything. Code looks like this:

inputRef.current.value="Some text"
{condition ? <SomeOtherComponent /> : <input type="text" ref={inputRef} />}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You have few options to fix that

  1. Remove ref on input and set value directly by value={'Some text'}.
  2. Render input and leave ref as it's but hide input by css based on condition.
  3. Create useEffect with listener on condition and when condition is falsy set the current.value.
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