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

126
Visualizações
Working with global document object in nextjs

We are using nextjs and trying to change the styling on a button click with document.getElementById but the styling does n't change irrespective of whatever we do.

pages/index.js

function Index() {
  
   function change(){
     console.log('changing');
     document.getElementById('navi').style.color='blue';
   }
   return (<Some test={change}> </Some>)

}

Now, there is another normal react-component outside pages directory and is as below.

function Some(prop){
 return (<div id="navi">
          <button onClick={prop.test}></button>
       </div>);
}

Now, whenever we click on the button it is executing the change() function in index.js and prints the console.log as 'changing' but the document.getElementById never works. Is there any specific reason for this, Please let us know.

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

0

you have to use of useRef instead of id and then on the onClick when you call prop.test you have to pass that ref as an argument like this:

function change(e){
  e.current.style.color="blue"
}

const navi= useRef(null)
<div ref={navi}>
          <button onClick={()=>prop.test(navi)}></button>
       </div>
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