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

109
Visualizações
Close menu onClick in React

My goal: To close my Navbar when clicking on the button which is inside the Navbar.

I can't seem to get it to work - If anyone can explain to me what I'm doing wrong that would be great!

Here's my Navbar:

import React, { useState } from 'react';

export default function Navbar() {
    const [showNav, setShowNav] = useState(true)


  return (
    <div isOpen={showNav} className="w-48 bg-gray-200 h-screen py-10">
        <button className="bg-primary w-32 h-12 rounded-lg text-white" onClick={() => setShowNav(false)}>Close Navbar</button>       
        <h1>Dashboard</h1>
        <h2>Menu item</h2>
        <h2>Menu item</h2>
        <h2>Menu item</h2>
    </div>
  )
}

Here is my App.js

import './App.css';
import Navbar from './components/Navbar';

function App() {

  return (
    <div className="flex">
      <Navbar />
    </div>
  );
}
export default App;
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

To achieve what you need, you can also use the condition like this:

return (
<>
  {showNav && <div className="w-48 bg-gray-200 h-screen py-10">
        <button className="bg-primary w-32 h-12 rounded-lg text-white" onClick={() => setShowNav(false)}>Close Navbar</button>       
        <h1>Dashboard</h1>
        <h2>Menu item</h2>
        <h2>Menu item</h2>
        <h2>Menu item</h2>
    </div>
   }
</>)

about 4 years ago · Juan Pablo Isaza Relatório

0

There is no such thing as isOpen property in div

If you want to hide the element rather than not render it

set display to none in style if showNav is false Like this:


import React, { useState } from 'react';

export default function Navbar() {
    const [showNav, setShowNav] = useState(true)


  return (
    <div style={{display: showNav ? 'initial' : 'none'}} className="w-48 bg-gray-200 h-screen py-10">
        <button className="bg-primary w-32 h-12 rounded-lg text-white" onClick={() => setShowNav(false)}>Close Navbar</button>       
        <h1>Dashboard</h1>
        <h2>Menu item</h2>
        <h2>Menu item</h2>
        <h2>Menu item</h2>
    </div>
  )
}

Or use other way to hide an element (like visibility for example) see here ways of hiding an element in CSS

if you don’t wanna render it at all

Use Snehasish Karmakar‘s answer

about 4 years ago · Juan Pablo Isaza Relatório

0

const [showNav, setShowNav] = useState(true) change to const [showNav, setShowNav] = useState("block");

onClick={() => setShowNav(false)} change to onClick={() => setShowNav("hidden")};

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