React Hamburger Menu not working as intended

I am facing an issue with my responsive hamburger menu. It is not functioning correctly when clicked on. The menu should display the navigation links and switch icons upon clicking, but it does neither. When I remove the line "{open && NavLinks}" and leave it as "NavLinks", the navigation links appear but the hamburger menu remains non-functional. How can I resolve this problem? Click here for a screenshot of the mobile navigation

import React, {useState} from 'react';
import NavLinks from './NavLinks';
import  './nav.css';
import {BiMenuAltRight} from 'react-icons/bi';
import {IoMdClose} from 'react-icons/io';

function MobileNavigation() {

    const [open, setOpen] = useState(false);

    const hamburgerIcon = <BiMenuAltRight className="Hamburger" size='50px' color="white" onClick={() => setOpen(!true)}/>;

    const closeIcon = <IoMdClose className="Hamburger" size='50px' color="white" onClick={() => setOpen(!true)}/>

    return (
        <nav className="MobileNavigation">
            {open ? closeIcon : hamburgerIcon}
            {open && <NavLinks />}
        </nav>
    );
}

export default MobileNavigation;
.nav-bar {
  height: 60px;
  width: 100%;
  top: 0;
  position: sticky;
  background-color: black;
  z-index: 1000;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
}

.nav-logo {
  display: flex;
}

.nav-logo h1 {
  font-size: 23px;
  display: flex;
  align-items: center;
  padding-left: 20px;
  font-family: "Earth-Obiter";
  background: linear-gradient(
    to right,
    #13c2e9,
    #3ab6ec,
    #5ca8e8,
    #799adf,
    #908bd0,
    #a480c7,
    #b873b9,
    #c866a6,
    #de5890,
    #ee4c73,
    #f4484f,
    #f05123
  );
  background-size: 100%;
  -webkit-background-clip: text;
  -moz-background-clip: text;
  -webkit-text-fill-color: transparent;
  -moz-text-fill-color: transparent;
}
.nav-logo img {
  object-fit: contain;
  width: auto;
  height: 50px;
  padding-left: 30px;
}

.nav-items {
  list-style-type: none;
  display: flex;
  flex-direction: row;
}

.nav-items li {
  padding: 0 30px;
}

.nav-items li a {
  align-items: center;
  color: white;
}

.li-quote a {
  background-color: rgb(0, 209, 157);
  border-radius: 20px;
  padding: 7px 15px;
}

.li-quote a:hover {
  background-color: #f05123;
  transition: ease-in-out 0.5s;
}

.nav-li a:hover {
  background-color: white;
  color: black;
  padding: 7px 14px;
  border-radius: 20px;
  transition: ease-in 0.3s;
}

.MobileNavigation {
  display: none;
}

/* Media Queries */

@media only screen and (max-width: 768px) {
  .Navigation {
    display: none;
  }
  .MobileNavigation {
    display: grid;
    flex-direction: column;
    background: rgb(0, 30, 60);
    align-items: center;
  }
  .Hamburger {
    position: absolute;
    right: 5%;
    cursor: pointer;
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

Answer №1

let menuIcon = <BiMenuAltRight className="Hamburger" size='50px' color="white" onClick={() => openSideMenu()}/>;

let exitIcon = <IoMdClose className="Hamburger" size='50px' color="white" onClick={() => closeSideMenu()}/>

Answer №2

const [isOpen, setIsOpen] = useState(false);

// Function to toggle the isOpen state
const handleToggle = () => {
  setIsOpen(!isOpen)
}

// Replace this...
const hamburgerIcon = <BiMenuAltRight className="Hamburger" size='50px' color="white" onClick={() => setIsOpen(!true)}/>;
const closeIcon = <IoMdClose className="Hamburger" size='50px' color="white" onClick={() => setIsOpen(!true)}/>

// ...with this
const hamburgerIcon = <BiMenuAltRight className="Hamburger" size='50px' color="white" onClick={handleToggle} />;
const closeIcon = <IoMdClose className="Hamburger" size='50px' color="white" onClick={handleToggle} />

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

Encountering an issue with locating an argument in a JSON file

My JSON file seems to have an error, and I'm not sure where the issue lies within my data blocks. I attempted using jsonpathfinder for reading but encountered a syntax error. { "data": [ { "gender": "male" } ] }{ "data": [ { ...

Error: Unable to locate module: 'cats.png' in the '/public/img' directory within Next.js

I am trying to implement the img tag in my Next app, but it doesn't seem to be working. This is how I have coded the img tag: <img src={'/public/img/cats.png'} alt="cats img" /> Despite searching extensively, I have been una ...

Determine the full location information with the help of Google Maps SDK without the need

My current challenge involves dealing with a list of unformatted and incorrectly written addresses. I am seeking a way to iterate through these flawed strings and generate more organized and accurate addresses using one of the many Google Maps SDKs availa ...

An error occurred when trying to set a cookie using Set-Cookie in a react application

Currently, I am immersed in a small project that involves user authentication via email and password before gaining access to their individual profiles. The backend operates on cookies which are established once the correct email and password combination i ...

403 Forbidden error occurs when AJAX value %27 is triggered

Stack Overflow has seen a multitude of inquiries related to apostrophes in form fields, particularly concerning unencoded values. An insightful post sheds light on the limitations of using encodeURIComponent(str) for handling apostrophes and suggests crea ...

How to achieve divs with see-through text using CSS?

How can I create a CSS (non-HTML5) based website with a filled div that has a cutout revealing an image underneath? There are various overlays and images involved, making the use of static images inconvenient. Additionally, I anticipate needing to scale th ...

Step-by-step guide on sending a JSON object to a web API using Ajax

I have created a form on my website with various fields for user entry and an option to upload a file. After the user fills out the form, my JavaScript function converts the inputs into a JSON file. I am attempting to send this generated JSON data along wi ...

Looking to transform a nested JSON structure into a visually appealing HTML table with merged rows?

My JSON structure appears as follows: $scope.data = [ { "type":"Internal", "count": 3, "library" : [ { "type":"Library 123", "count": 2, "version" ...

Guidelines for incorporating JS in Framework7

I am developing an application using the framework7. I am facing a challenge where I need to execute some javascript in my page-content, but it is not running as expected. <div class="pages"> <div class="page close-panel" data-page="item"> ...

Activate fancybox when clicked, triggering numerous ajax requests

Although I achieved my desired output, the method in which it was done is not ideal because AJAX duplicates with every click. Check out my code: <a href="/messages/schedule" class="greenbtn fancybox">Schedule</a> $('a.fancybox').cl ...

Updating the @mui/x-data-grid table dynamically upon fetching new data

Seeking assistance regarding updating data in the DataGrid component from the @mui/x-data-grid module within a React application. Specifically, I am facing challenges in refreshing the table after retrieving data from an API using react-query. Despite succ ...

Getting the most out of fonts with Webpack sass-loader

I recently set up a custom font in my project like this: $font-path: '~@/assets/fonts/'; @font-face { font-family: 'mainFont'; font-weight: 300; font-style: normal; src: url('#{$font-path}mainFont/mainFont.eot&apos ...

Switch the active input focus between components from another component

I'm working on the following code: <Wrapper> <InputsGroup /> <Controls /> </Wrapper> Inside the InputsGroup component, I have a set of input elements: // InputsGroup <> <input ... /> <input ... /& ...

"Utilizing Vue.js to determine whether a checkbox is filled with data or left

My goal is to create a checkbox using vue js without writing a method. I want the checkbox to default to false, and when checked, I want the data "opening_balance" to be an empty array. Conversely, if the checkbox is unchecked, I want it to be omitted when ...

Steps to improve the appearance of the Header on a dataTable during Dragging

I am working on creating a table that can be reordered using mat-table and MatTableDataSource. However, I would like the column to have a white background color instead of being transparent when dragged. Is there a way to achieve this? <table mat-tab ...

Styling with CSS to create a layout similar to Facebook Messages

I am attempting to design a web layout similar to Facebook's Messages page, with a fixed height header and footer, and a flexible height main element. Within the main element, there needs to be: A div (100% height of the main elem.) that is scrolla ...

Can you explain the distinction between a synchronous and asynchronous request in terms of their async parameter values (true/false)?

Can you explain the difference between setting async=false and async=true when utilizing the open method of the XMLHttpRequest? function GetXML() { if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new X ...

Disable the enter key from closing the alert box

Is there a way to ensure that a user must manually close a JavaScript alert, preventing them from simply closing it by pressing enter? (It may sound suspicious, but in the application users frequently press enter and I need to make sure they don't ov ...

Personalize the hover effect and underline style of links in material-ui

I am currently delving into the world of material-ui custom styles and came across this helpful resource. However, I find myself a bit lost in how to properly override the classes using withStyles. <Breadcrumbs aria-label="breadcrumb" separator=" ...

Tips for selecting React component props based on a specific condition

Here is a React component that I have: <SweetAlert show={this.props.message} success title={this.props.message} onConfirm={this.props.handleCloseAlert}> </SweetAlert> Upon using this component, I receive the following alert ...