The hamburger icon refuses to close even after being clicked once

Navigation Bar Component

import { Link } from 'react-router-dom';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { logout } from '../../redux/actions/auth';
import './Navigation.styles.css';
import ham from './assets/ham.svg';
import exit from './assets/exit.svg';

export const NavigationBar = ({ auth: { isAuthenticated, loading }, logout }) => {
  const [isMenuOpen, setIsMenuOpen] = useState(false);

  const authLinks = (
    <div className='buttons'>
      <Link to='/'>
        <button onClick={logout} className='button'>
          Logout
        </button>
      </Link>
    </div>
  );

  const guestLinks = (
    <div className='buttons'>
      <Link to='/register'>
        <button className='button'>Register</button>
      </Link>
      <Link to='login'>
        <button className='button'>Login</button>
      </Link>
    </div>
  );
  console.log(isMenuOpen);
  return (
    <div className='container'>
      <header>
        <h2>
          <Link to='/' className='logo' alt='Escapebe logo'>
            <i className='fas fa-microphone'></i> Escapebe
          </Link>
        </h2>
        <nav>
          <Link to='#' className='hide-desktop'>
            <img
              src={ham}
              alt='toggle menu'
              className='menu'
              onClick={() => setIsMenuOpen({ isMenuOpen: !isMenuOpen })}
            />
          </Link>
          <ul
            className={
              isMenuOpen
                ? 'hide-desktop show-mobile'
                : 'show-desktop hide-mobile'
            }
          >
            <li className='exit-btn hide-desktop'>
              <img
                src={exit}
                onClick={() =>
                  setIsMenuOpen({
                    isMenuOpen: !isMenuOpen
                  })
                }
              />
            </li>
            <li>
              <Link to='/'>News</Link>
            </li>
            <li>
              <Link to='/'>Groups</Link>
            </li>
            <li>
              <Link to='/'>About</Link>
            </li>
            <li>
              <Link to='/'>FAQ</Link>
            </li>
            {!loading && (
              <Fragment>{isAuthenticated ? authLinks : guestLinks}</Fragment>
            )}
          </ul>
        </nav>
      </header>
    </div>
  );
};

NavigationBar.propTypes = {
  logout: PropTypes.func.isRequired,
  auth: PropTypes.object.isRequired
};

const mapStateToProps = state => ({
  auth: state.auth
});

export default connect(mapStateToProps, { logout })(NavigationBar);

MainSection.styles.css

.container {
  text-align: center;
  padding: 0.8em 1.2em;
  color: #d1d0d0;
}

.button {
  background-color: #4caf50;
  border: none;
  width: calc(100% - 1em);
  display: block;
  color: #d1d0d0;
  border-radius: 20px;
  padding: 0.5em;
  text-decoration: none;
  font-size: 1em;
  margin: 3% auto 7%;
  position: relative;
  z-index: 4;
  cursor: pointer;
  box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}

.button:hover {
  background-color: green;
  color: white;
}

.triangle {
  margin: 2em auto 2em 45%;
  width: 70%;
}

h1,
.subhead {
  position: relative;
  z-index: 3;
}

.subhead {
  font-size: 1.1em;
}

@media only screen and (min-width: 650px) {
  .triangle {
    width: 50%;
  }

  .button {
    width: 35%;
  }

  h1 {
    font-size: 2em;
    margin: 0;
  }

  .subhead {
    font-size: 1.4em;
    margin-bottom: 12%;
  }
}

@media only screen and (min-width: 1000px) {
  .button {
    width: 35%;
  }

  .container {
    width: 80%;
    margin: 0 auto 13% auto;
  }

  .hide-desktop {
    display: none;
  }

  .show-desktop {
    display: block;
    margin: 0 auto 13% auto;
  }

  nav ul {
    position: inherit;
    width: auto;
    background: none;
    height: auto;
    display: flex;
    padding-top: 0;
  }

  nav ul li {
    float: left;
  }

  nav ul li a {
    color: black;
    background-color: inherit;
    text-align: right;
    padding: 1em 2em;
  }

  nav ul li a:hover {
    background-color: inherit;
  }
}

@media only screen and (min-width: 1200px) {
  .container {
    width: 70%;
  }
}

Navigation Logo styles may be causing issues with the functionality of your hamburger menu. Try checking your CSS for any conflicting or incorrect styling that could impact the behavior.

Answer №1

Toggle the menu by calling setIsMenuOpen with the opposite value of isMenuOpen

The correct way to do this is:

setIsMenuOpen(!isMenuOpen)

I believe this solution will solve the issue.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How come the mongoose ref feature is not functioning properly for me when I attempt to reference objects from a different schema?

I'm having trouble accessing the attributes of a store's address, as I keep getting 'undefined'. It seems that the address is only an id, even though I set up a 'ref' in the address schema. What could be causing this issue? H ...

Div positioned absolutely beneath the footer

I'm facing an issue where the footer field I add under a div with an absolute value escapes on both mobile and desktop. Additionally, the iframe does not have full height. Any suggestions on what I should do? All the specifics are provided in the att ...

Steps for transforming an array into a complex array

Here is an array I have: [2, 1, 2, 1, 1, 1, 1, 1] I am looking for a way to create new arrays within the original array when the sum of values exceeds four. The desired result should look like this: [[2,1],[2,1,1],[1,1,1]] ...

I developed a digital Magic 8 Ball program, but unfortunately, it's only providing me with one response

I'm currently working on a Discord Bot for my friends and myself. One of the scripts I've created is an 8Ball script, but it's only giving me one answer. Here's the code snippet for my variable: var rand = ['Yes', 'No&apo ...

Restricting the number of mat-chips in Angular and preventing the input from being disabled

Here is my recreation of a small portion of my project on StackBlitz. I am encountering 4 issues in this snippet: I aim to restrict the user to only one mat-chip. I attempted using [disabled]="selectedOption >=1", but I do not want to disable ...

Top method for independently scrolling overlapping elements in both the x and y directions

Sorry if this is repeating information. I have a structure of nested divs like this: -container -row In order to enable scrolling without the default scrollbar appearing, each container and row has an additional container. My goal is to be able to scrol ...

Learn how to use the Firebase Adapter for Next Auth to easily sign in using your email and password

I am currently using next-auth along with a Firebase adapter for authentication, but I am uncertain about the correct way to sign in users. I do not want to utilize my Google account for signing in; instead, I have user accounts within a Firebase project a ...

Dynamic jQuery slideshow with unique starting point

My photo collection is displayed in a list format like this: <ul class="slideshow"> <li><img src="images/slideshow/slide0.jpg" alt="" /></li> <li><img src="images/slideshow/slide1.jpg" alt="" /></li> & ...

When making an XMLHTTPRequest, Chrome often responds with an "Aw, snap"

In the development of my server-based multiplayer game using three.js, I have set up the server to operate as follows: game->Server2.php->Server.txt game->Server.php->Server.txt->game The process flow is as follows: 1.) The game is gener ...

Tips for creating a high-performing algorithm to locate a specific word within a JSON file

I am in the process of creating a word game that involves users typing letters on a board to form meaningful words. If the typed word matches any word in a JSON file, the user earns a point. I have successfully implemented the basic functionalities of the ...

Angular 4 showcases the information stored within this dataset

The data returned from an API to my Angular 4 application is not to my liking. Here is an example of the JSON, where I am only interested in the coin and its price: Goal is to display this data on the page: Coin Price BTC $4,281.28 ETH $294.62 ...

Attention! Are you aware of the potential consequences of the impending phase-out of the name attribute in W3C validation? Have you considered how this development

Any thoughts on what could replace the name attribute? According to the W3C validator... Validation Output: 4 Warnings Here is a list of the warning message(s) generated while checking your document. Warning Line 12, Column 21: The name attribute i ...

Adjust the color of the sidebar's list items when scrolling

How can I change the background color of left sticky sidebars li on scrolling? When scrolling through BMW, BMW's background color in the sidebar should turn green. Please refer to the code snippet provided below. The background color of the li ...

The data displayed in the <span> element isn't reflecting the response from the loaded page, despite being visible in Firebug

I have encountered a problem while trying to load a signup.php page into a div on my main page. All the elements (forms, JavaScript, etc.) function correctly in the loaded page, except for one issue - I cannot get the response from the PHP script to displa ...

Reaching the maximum request threshold

Currently, I am facing an issue where users are able to upload files from the client side (using Angular 4) to the server (implemented with Spring Boot). The problem arises when a user attempts to upload more than 6 files at once. In such cases, Chrome uti ...

Assign value to an element in an array using the value from another element

Is it possible in Javascript to set the value of one array element based on another without changing both elements? Specifically, how can I only modify arr[1] while leaving other elements unchanged? arr[0] = {i: 0}; arr[1] = arr[0]; arr[1]['summ&apos ...

flickering image when shared using react-navigation-fluid-transitions

Currently, I am in the process of mastering the art of creating stunning animations using react native. However, I am facing some difficulties while working with the react-native-fluid-transitions library. My issue arises when using shared elements with i ...

Issue with collapse functionality in Bootstrap not activating after using append()

Whenever the ajax call is successful, I am appending the content to the element with the id #special-contents. However, upon page load, the collapse button does not expand the panel. Below is my JavaScript code: $.ajax({ url: `${URL}/promotions/`, ...

Utilizing CSS to apply unique styles to individual radio buttons

I have 4 radio buttons in my quiz application. I need to style only one of them, which is the correct answer option. Here's the HTML snippet that was generated: <p> <input id="SelectedAnswer" type="radio" value="5" name="SelectedAnswer"> ...

Is Array.sort() ineffective when dealing with extensive arrays?

Why is Array.sort() failing on large arrays? Did I overlook something? I have double-checked to ensure that I'm not accidentally sorting strings. I'm struggling to comprehend why this is failing Here is the code snippet I pasted from Chrome Debu ...