Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

228
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda