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

Is ngSwitch in Angular causing the label element to disappear?

I am using AngularJS to generate form elements dynamically based on an array containing form details such as input type and value. For example, here is the code snippet I have for creating a text input field: <div ng-repeat="input in input_array"> ...

Executing the main process function

One of the challenges I'm facing is calling a function from the main process in Javascript while a button is clicked in another file. Here is the code snippet: Main.js const electron = require( 'electron') const {app, BrowserWindow} = elec ...

Adaptable Navigation

I am currently working on creating a responsive navigation menu inspired by Bootstrap's design. However, I have encountered some challenges in implementing it. The current issue I am facing is that the navigation disappears when transitioning from a s ...

Angular 5 offers the ability to incorporate dynamic checkbox input into your application

Here is my code snippet: <input [type]="'checkbox'" [(ngModel)]="inputValue"> <p>Value: {{ inputValue }}</p> I'm puzzled as to why the value in inputValue remains unchanged. Can anyone shed light on this? I am unable to ...

Rearrange items in a display: contents wrapper using CSS Grid

I'm having trouble positioning descendants in a root grid while they are enclosed in a container with display: contents. The issue I am facing is that one element is not being correctly placed within the grid: .wrapper { width: 80ch; margin: a ...

What is the best way to enlarge a thumbnail using CSS?

Is there a way to resize an image without distortion? I am struggling with resizing a 70px thumbnail image to 200px X 165px while maintaining the aspect ratio. Currently, when I set the width to 200px and height to 165px, it stretches the image. Below is ...

JavaScript - Transforming Name:ItemName/Value:ItemValue Pairs into Standard ItemName:ItemValue JSON Format

Looking to reformat JSON data? [{"name":"age","value":31}, {"name":"height (inches)","value":62}, {"name":"location","value":"Boston, MA"}, {"name":"gender","value":"male"}] If you want it to be in a different format: [{"age": 31}, {"height (inches)": 6 ...

Troubleshooting NPM start issues with React on Windows 10 and Ubuntu

Following the update of nodeJS to version 16.13.1 and the initiation of a project using: npx create-react-app... cd....... npm start The error encountered is as follows: Html Webpack Plugin: Error: Child compilation failed: Module build failed ...

Making a Zoom effect using p5.js

I have searched for a solution to this question multiple times, but none of the answers I came across seem to work for me. Currently, I am able to allow the user to scale an image with a simple scale(factor) call. However, now I am attempting to implement ...

How can I utilize FileReader with Next.js server-side rendering?

I'm currently working on a Next.js app, and I am facing an issue while trying to use FileReader in order to read the contents of an uploaded file. This is how my component is structured: let fileReader; let props = { multiple: false, onRe ...

Adding to object in an external JSON file using javascript

I am working with an external file called file.json which contains the following values: { "number": "value" } My goal is to run a function that will append new data to the existing file instead of overwriting it. For instance, after running the func ...

send back the result to the primary function

I need help with this code. I'm trying to return the budget from callbacks in the main function. How can I return a value from the main function? // This method returns the current budget of the user getCurrentBudget: function (req) { var reqTok ...

How can I alter the icon's color?

Is it possible for the icon's color to change to red when the condition is greater than 0, and to gray when the condition is equal to zero? <TouchableOpacity onPress={() => { if (Object.values(selectedIt ...

The button colors in Material Ui do not update properly after switching themes

React Material UI is being utilized in this project. Although the theme is being overridden, the colors of buttons and inputs remain unchanged. Here is how the Material UI theme is created in theme.js // @flow import { createMuiTheme } from '@materi ...

"Accessing and updating the current navigation state using jQuery/ajax when clicked

Currently, I'm working on a website project where I am using a jquery/ajax script to dynamically pull in content from another page with a fade effect when clicking on a tab in the navigation menu. You can check out the effect on the page here. Only th ...

Using styled-components to enhance an existing component by adding a new prop for customization of styles

I am currently using styled-components to customize the styling of an existing component, specifically ToggleButton from material ui. However, I want my new component to include an additional property (hasMargin) that will control the style: import {Toggle ...

The most convenient method for automatically updating Google Charts embedded on a webpage

I am facing an issue with refreshing a Google Graph that displays data from a MySQL database. The graph is being drawn within a webpage along with other metrics: Data Output from grab_twitter_stats.php: [15, 32], [14, 55], [13, 45], [12, 52], [11, 57], [ ...

Using MIPS Assembly Language: Displaying Register Content on Screen

I'm working on replicating a simple form of debugging in MIPS similar to javascript. I am wondering how I can achieve the equivalent of this ($t0 represents a javascript variable here): console.log($t0); In simpler terms, I am looking for the metho ...

Merge the throw new Error statement with await in a single expression

Is it possible to combine throwing an error and using the await keyword in one statement using the AND operator? The code snippet below demonstrates my intention: throw new Error() && await client.end(). So far, this approach has been working wel ...

Customize the appearance of parent components based on the content of their child elements

I am currently working on a component that contains multiple instances, each with its own unique internal component (acting as a modal wrapper). I need to customize the size of each modal instance independently. How can I achieve this customization when th ...