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

107
Views
How to toggle style property using UseState

In react I would like to lock scroll-bar when sub-menu is out and then again unlock it when menu is hidden. I press the same button to open and close sub-menu (adds and removes translate for this element). With what i did page sucessfully locks but when i press menu button and sub-menu dissapears the page is still locked, how can i change that the code is below:

const [isActive, setActive] = useState(false);
const toggleClass = () => {
    setActive(!isActive);
    document.body.style.overflow = "hidden"
  };

 <div className="button"
   onClick={toggleClass}
   className={
   isActive ? "nav-button closebtn active" : "nav-button closebtn"}>
   <span></span>
 </div>

how can I toggle the value of overflow from hidden to unset by pressing the button, just like i do with adding the class when i press the same button

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Use toggle and add style in CSS file.

import './myStyle.css';
const [isActive, setActive] = useState(false);
const toggleClass = () => {
    setActive(!isActive);
    document.body.classList.toggle('lock', isActive); // <-- Add this
  };

 <div className="button"
   onClick={toggleClass}
   className={
   isActive ? "nav-button closebtn active" : "nav-button closebtn"}>
   <span></span>
 </div>

// myStyle.css

.lock {
   overflow: hidden;
}
about 4 years ago · Juan Pablo Isaza Report

0

I believe that in your togle you have to validate if it is active or not and in that change the hidden to visible

Something like:

const toggleClass = () => {
    setActive((active) => {
      if(active){
          document.body.style.overflow = "visible";
          return !active;
      }
      document.body.style.overflow = "hidden";
      return !active;
    });
    
};
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!