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

112
Visualizações
Implementing JavaScript code to ReactJs logic doesn't work

I am new to react and i need some help.

I am trying to implement some hamburger menu icon that someone put on codepen:

https://codepen.io/rss/pen/OJxZrR

The problem is that i can't get animation to work.

import "../../index.css";

const [style, setStyle] = useState("nav-container");


const changeStyle = () => {
setStyle("pushed")};

I tried something like this and I added button onClick for changing state but it doesn't work

       <button
          onClick={() => {
            changeStyle();
          }}
        >
          <div id="nav-container">
            <div className="toggle-icon">
              <span className="bar"></span>
              <span className="bar"></span>
              <span className="bar"></span>
            </div>
          </div>
        </button>
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can use the state as a toggle and set the class pushed if the state is true

import "./styles.css";
import { useState } from "react";

export default function App() {
  const [pushed, setPushed] = useState(false);

  const changeStyle = () => {
    setPushed(!pushed);
  };
  return (
    <button
      onClick={() => {
        changeStyle();
      }}
    >
      <div id="nav-container">
        <div className={`toggle-icon ${pushed ? "pushed" : ""}`}>
          <span className="bar"></span>
          <span className="bar"></span>
          <span className="bar"></span>
        </div>
      </div>
    </button>
  );
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You just need to toggle pushed class on nav-container div

Change your onClick handler to this

const changeStyle = () => {
  const navContainer = document.getElementById("nav-container");
  navContainer.classList.toggle("pushed");
};
about 4 years ago · Juan Pablo Isaza Relatório

0

Some precautions -

  • Make sure your button hover make the cursor pointer. Otherwise can have low z-index problem
  • const changeStyle = (e) => { e.preventDefault(); setPushed(!pushed); };
  • There is no content inside it. So make sure it's existence.
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