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

172
Views
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 answers
Answer question

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 Report

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 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!