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

230
Views
React: how to execute two functions at the same time when one is dependent on the other

i have a collabsable sidebar that i'm building with react and some menus have dropdowns, what would be the best approch to collapse the dropdown menus when the sidebar itself is called to collapse i have to boolean's for the sidebar and the menus within the sidebar

this is the basic function that collapses the sidebar

const [open, setOpen] = useState(false) //for the menus
const [collapse, setCollapse] = useState(false) //for the sidebar


  function handleCollapse() {
    setCollapse(!collapse);
   }

i'm calling the function from a button onClick={handleCollapse}

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

0

Just add an effect that gets triggered when collapse changes

useEffect(() => {
  if(collapse) setOpen(false)
}, [collapse])

btw, you can use functional state update to access previous collapse:

function handleCollapse() {
  setCollapse(prevCollapse => !prevCollapse);
}
about 4 years ago · Juan Pablo Isaza Report

0

I see 2 ways

  1. Change your state to object
const [state, setState] = useState({ open: false, collapsed: false })

function handleCollapse() {
   if (!state.collapsed) {
     setState({ open: false, collapsed: true });
   } else {
     setState({ open: true, collapsed: false });
   }
}
  1. useReducer with a similar functionality as useState

I would avoid useEffect - it would require state update twice.

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!