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

545
Visualizações
How to access dom element that is not inside JSX in React?

We are using ReactJS framework, so we don't find element written in the entire code. The only way is to find the DOM element and should set the attribute. I am doing this in app.js.

setTimeout(function () {
  const formElement = document.getElementsByTagName('form')
  formElement.item(0).setAttribute('autocomplete', 'off')
}, 1000)

The above solution is working fine, but if the page loading is slow, then this will not set the attribute. Is there any other way to find the element and set the attribute? I have also tried to set it using the below code after DOMContentLoaded, but it's not working. Nothing inside this event is working.

document.addEventListener('DOMContentLoaded', () => {
   const formElement = document.getElementsByTagName('form')
   formElement.item(0).setAttribute('autocomplete', 'off')
})
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

In React a good way to access DOM elements is by using useRef hook. However, if that element you want to query is not in the JSX of your current component, the useEffect hook could be used with normal JavaScript DOM methods. Like so:

useEffect(()=>{
  const formElement = document.getElementsByTagName('form')
  formElement.item(0).setAttribute('autocomplete', 'off')
},[]);
about 4 years ago · Juan Pablo Isaza Relatório

0

You don't find the element because 'DOMContentLoaded' gets fired when HTML file completes loading, not when React renders the form.

How about using MutationObserver instead? https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver

Also doing that in root level app.js sounds wrong. Can you do it in the component that is closest to the container of the form?

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