Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

237
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda