I'm trying to access my navigation bar, but for some reason, it's not allowing me to do so

Having trouble getting the navigation bar to open. I have set it up so that when clicked, it should remove the hide-links tag, but for some reason, it does not toggle properly and keeps the ul hidden.

import React from "react";
import { NavLink } from 'react-router-dom';
import '../styles/navstyle.css'
import { MdMenu } from 'react-icons/md';
import { MdClose } from 'react-icons/md';
import { useState } from 'react';

export default function NavMenu () {
    const [ open, setOpen ] = useState(false);

    return(

        <div className="nav">
            <div className="moblie-menu-icon"
                onClick={() => setOpen(!open)} 
                role="button"
                >
                <MdMenu />
            </div>
            <ul className={ !setOpen ? 'nav-links' : 'nav-links hide-links'}>
                <div className="closeNavIcon" 
                onClick={() => setOpen(!open)} 
                role="button" 
                >
                    <MdClose />
                </div>
                <li className="link">
                    <NavLink to="/" exact>Home</NavLink>
                </li>
                <li className="link">
                    <NavLink to="/about">About</NavLink>
                </li>
                <li className="link">
                    <NavLink to="/projects">Projects</NavLink>
                </li>
                <li className="link">
                    <NavLink to="/contact">Contact</NavLink>
                </li>
            </ul>
        </div>
    );
}

Include the CSS if needed below:

.nav {
    position: fixed;
    z-index: 100;
    top: 0;
    left: 0;
    width: 100%;
    padding: 1rem 0;
    background-color: var(--dark-bg);
}

ul {
    display: flex;
    justify-content: center;
    align-items: center;
    max-width: 1200px;
    margin: 0 auto;
    width: 90%;
}

... (more CSS rules)

I've attempted various solutions like switching classes or adding a show-links class upon click, but none seem to achieve the desired result.

Answer №1

The problem lies with the line below:

<ul className={ !setOpen ? 'nav-links' : 'nav-links hide-links'}>

Replace !setOpen with !open and it will function correctly.

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 it possible to activate or deactivate a button depending on a radio button selection using jQuery?

Below is the code I have written in an aspx file: <asp:Button ID="btn" runat="server" Text="Add As a Sub Organization" OnClick="lnkChild_Click" ValidationGroup="GroupA" class="btn-u" CausesValidation="true" /> <asp:GridView ID="grdDuplicateO ...

Is it possible for AJAX JSON response to return both accurate and undefined values sporadically?

In the process of developing JavaScript code to pinpoint the smallest unused number within a specified range of numbers, I encountered an issue with the AJAX request. Despite successfully retrieving all used numbers in the designated range, some undefined ...

Can flexbox elements be animated as they scroll?

Wondering if it's feasible to animate flex elements upwards when scrolled? Attempting to replicate this effect: https://codepen.io/Sergiop79/pen/bxjGEe I want to apply this to the elements below (styled in flexbox), either the entire "row" or each i ...

Asynchronous data fetching adding two entries to an array

I've been experimenting with making API calls for Rick & Morty using fetch and async arrow functions, but I've run into an issue where the elements are being added to my array twice. I've tried calling the function both with and without useE ...

jQuery custom slider with advanced previous and next navigation capability for jumping multiple steps

I am currently learning jQuery and programming in general. Instead of using a pre-built plug-in, I decided to create my own image slider/cycle from scratch to keep the code concise and improve my skills. My function goes through each li item, adding a &ap ...

Want to learn how to create an image magnifier using just one image?

At first, I created an image magnifier that would zoom in when the user hovered over the image. However, now I am looking to switch to a lens zooming method that uses only one image. ...

What is the best approach to manage the package versions of react, react-dom, @types/react, and @types/react-dom?

Recently, I decided to update the versions of react/react-dom in my project from 16.3.2 to 16.8.6 so that I could start using hooks. Surprisingly, after making some minor adjustments to my code, the update went smoothly. However, since we are utilizing ty ...

Is it possible for an HTML number input's arrows to change in increments that are different from its designated step attribute?

Within my design, I have implemented an <input type="number" step="1"> element for selecting font size. However, in the context of working on a large canvas, adjusting the number by 1 is hardly noticeable. It would greatly enhance ...

What is the process for assigning a function and its arguments as a callback in programming?

Here is a code snippet for your consideration: $scope.delete=function(){ foo('x',3); }; How can we improve the clarity of this code snippet when the callback function contains only one line that calls another function? It's important ...

Interacting with a 3D model using the mouse cursor in a three

After stumbling upon the three.js library recently, I've been spending several days experimenting with it. I am eager to incorporate a mouse event into my project similar to this example where the head of the skull follows the cursor. However, I want ...

Issues with the proper functionality of the .ajax() method in Django

I'm currently facing an issue with my Ajax code while trying to interact with the database in order to edit model instances. I noticed that the first alert statement is functioning correctly, however, the subsequent alert statements are not working as ...

Problems Arising from the Combination of Django and External JavaScript

I am currently working on a project that uses Django 1.11 and JavaScript, but I am encountering an issue. The JavaScript code works fine when run within the HTML file, as it successfully loads the JSON data. However, when I move everything to the static fo ...

Understanding the Execution of Asynchronous Code

I've been grappling with this problem for a while now, but I just can't seem to find the solution. That's why I'm reaching out for your expertise. Consider the example below: const async = require('async') var counter = 0 v ...

Utilizing a functional component within a React Native class component

Hello there! I have a functional component that works perfectly on its own. Here it is: function MeditationList({route, navigation}) { const address = route.params; return ( <View> <HeaderBar /> <Text> You ha ...

The console experienced a forced reflow when a tooltip appeared on the slider handle while executing JavaScript

I am currently facing an issue with my web page that includes various elements along with the Ant.design slider. The values of the slider are being controlled through React states. However, I have noticed that when the slider tooltip is enabled, the respon ...

Encountering issues while attempting to access MuiThemeProvider

An error occurred: MuiThemeProvider(...): Render function did not return anything. This typically indicates a missing return statement, or to render nothing, return null. I am not very familiar with material UI, so I decided to open the terminal and run &a ...

What could be causing this strange striping effect in the radial gradient?

What causes the striped effect in the radial gradient code provided on this website: click here to view body { background: rgba(216,215,221,1); background: -moz-radial-gradient(center, ellipse cover, rgba(enter code here216,215,221,1) 0%, rgba(0,9 ...

Guide on showing a component exclusively for iPads with React and TypeScript

I need help displaying an icon only in the component for iPad devices, and not on other devices. As a beginner in coding for iPads and mobile devices, I am unsure how to achieve this specific requirement for the iPad device. Below is the code snippet tha ...

Tips for maintaining the persistent state of tabs in a React.js application

I have created dynamic tabs using an array list, including nested tabs within those tabs. You can view the live sandbox link here: For the sandbox code and preview, visit: https://codesandbox.io/s/black-glade-phs69 The setup consists of 3 main tabs ...

`How can I enable the download attribute feature on Safari browser?`

Is there a workaround for saving files with a specified name in Safari? The following HTML code does not work properly in Safari, as it saves the file as 'unknown' without an extension name. <a href="data:application/csv;charset=utf-8,Col1%2C ...