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

201
Visualizações
React conditional toggle working only once

In the project i am working on, I am trying to implement a Hamburger Menu functionality using React. I decided to toggle a boolean (useHamburger) using the function TurnToHamburger. These would change the stylesheet of the navbar so that it would look like a dropdown menu. Problem is, it would only toggle once and after that the appearance of the navbar does not change at all. How do i solve this?

Below is the relevant code.

import {useState} from 'react'
const Navbar = ({img}) => {
   const navbarinitial = {
    listStyle: 'none',
    display: 'flex',
    justifyContent: 'space-around',
    flexDirection:'row',
    width: '30%',
    color: 'var(--GrayishViolet)'
   };

   const navbarfinal = {
    listStyle: 'none',
    display: 'flex',
    flexDirection:'column',
    justifyContent: 'space-around',
    width: '30%',
    color: 'var(--GrayishViolet)',
    backgroundColor:'red'
   }

   let useHamburger = false;
   const [navbarstyle, setNavbarStyle] = useState(navbarinitial);



   const TurnToHamburger=()=> {

       if (!useHamburger) {
        setNavbarStyle(navbarfinal)
        useHamburger = true;
       }
       else {
           setNavbarStyle(navbarinitial)
           useHamburger = false;
       }
   }







about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

In the useState() hold the current status of the menu (hamburger). The state should be a Boolean - hamburger open or not. Toggle this state to change the menu. The selection of the style object should be derived from the current value of the hamburger state.

const { useState } = React;

const navbarinitial = {
  listStyle: "none",
  display: "flex",
  justifyContent: "space-around",
  flexDirection: "row",
  width: "30%",
  color: "var(--GrayishViolet)",
};

const navbarfinal = {
  listStyle: "none",
  display: "flex",
  flexDirection: "column",
  justifyContent: "space-around",
  width: "30%",
  color: "var(--GrayishViolet)",
  backgroundColor: "red",
};

const Navbar = () => {
  const [hamburger, setHamburger] = useState(false);

  const toggleHamburger = () => setHamburger((prev) => !prev);

  return (
    <nav>
      <div className="nav-right">
        <ul
          className="nav-options"
          style={hamburger ? navbarfinal : navbarinitial}
        >
          <li>Features</li>
          <li>Pricing</li>
          <li>Resources</li>
        </ul>
        <div className="navbar-buttons">
          <button onClick={toggleHamburger}>Toggle Burger</button>
        </div>
      </div>
    </nav>
  );
};

ReactDOM.render(<Navbar />, root);
<script crossorigin src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>

<div id="root"></div>

about 4 years ago · Juan Pablo Isaza Relatório

0

The reason why it doesn't work is because any variable that doesn't use useState will be reset each time the component renders. Do this instead:

const [hamburger_open, setHamburgerOpen] = useState(false);

const TurnToHamburger=()=> {
   if (!hamburger_open) {
       setNavbarStyle(navbarfinal);
       setHamburgerOpen(true);
   } else {
       setNavbarStyle(navbarinitial);
       setHamburgerOpen(false);
   }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

const toggleHamburger = () => setHamburger(!prev);

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