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

133
Views
How to dynamically change the state in useEffect

I have a useEffect function that finds an element by it's ID and then if the element has a certain class then it set's the state to be either true or false. However this only happens on the initial load so if I was to click on the button (which is within the FoodList component) that removes/adds the class, then nothing happens. What's the best way to make this dynamic?

const [filterExpaned, setFilterExpanded] = useState<boolean>();

useEffect(() => {
    const listPanelToggle = document.getElementById('list-panel-toggle');

    if (listPanelToggle?.classList.contains('list-panel-toggle--collapsed')) {
        setFilterExpanded(true);
        console.log('It does have the class');
    } else {
        setFilterExpanded(false);
        console.log('It does not have the class');
    }
}, []);

return (
  <>
    <FoodList /> // The id 'list-panel-toggle' is within this component
    </>
);
about 4 years ago · Juan Pablo Isaza
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!