Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

132
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!