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

180
Visualizações
How to convert vanilla to react hook toggle menu

I am trying to get a code from vanilla js with bootstrap and use react hooks on yet. I watched some videos on youtube about useState and useReff, however, these concepts still are a bit confusing when I try to apply them in my projects.

I am trying to convert a toggle function in vanilla to react hooks. Can you please guide me on how to start thinking to change this code?

Thanks in advance.

Vanilla:

window.addEventListener("DOMContentLoaded", (event) => {
  // Toggle the side navigation
  const sidebarToggle = document.body.querySelector("#sidebarToggle");

  if (sidebarToggle) {
    if (localStorage.getItem('sb|sidebar-toggle') === 'true') {
        document.body.classList.toggle('sb-sidenav-toggled');
    }
    sidebarToggle.addEventListener("click", (event) => {
      event.preventDefault();
      document.body.classList.toggle("sb-sidenav-toggled");
      localStorage.setItem(
        "sb|sidebar-toggle",
        document.body.classList.contains("sb-sidenav-toggled")
      );
    });
  }
});

React:

const [inactive, setInactive] = useState(false);

  useEffect(() => {

  })


about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

useEffect is fired after Components have been painted so no need to call DOMContentLoaded event listener

const Component = (e) => {
     const [inactive, setInactive] = useState(false);
     
     useEffect(() => {
          // insert code here
     }, [inactive]);
} 
about 4 years ago · Santiago Trujillo Relatório

0

You can do something like this :

import { useState, React } from 'react';

const YourComponent = () => {
  
  // isSidebarOpen: value, setIsSidebarOpen: setter, false by default.
  const [isSidebarOpen, setIsSidebarOpen] = useState(false);
  
  const toggleSideBar = () => {
    // true -> false / false -> true
    setIsSidebarOpen(!isSidebarOpen);
  }
  
  return (
    <YourButton onClick={toggleSideBar}>
      Text
    </YourButton>
  );
};

about 4 years ago · Santiago Trujillo 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