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

css - flexible div height based on content

Issue encountered: In my website, I have a dynamically generated content inside a specific div. On the left side of the provided URL, you can see the current appearance of the div. It is worth noticing that the height of the container div (mainContent_mid ...

What is the process of enabling scrolling on the main panel once scrolling on the sidebar has concluded?

How can I achieve a scrolling behavior similar to the one demonstrated here? When scrolling through the sidebar, I want it to continue scrolling until it reaches the end. After that, any further scrolling on the sidebar should scroll the main panel inste ...

The combination of sass-loader and Webpack fails to produce CSS output

Need help with setting up sass-loader to compile SCSS into CSS and include it in an HTML file using express.js, alongside react-hot-loader. Check out my configuration file below: var webpack = require('webpack'); var ExtractTextPlugin = require ...

Having trouble accessing env variables from React Component in Next.js?

I recently set up a Next.js project and included an .env file to store environment variables used in my server.js file. However, I am facing an issue when trying to access these variables from a component. Can anyone provide guidance on how to resolve this ...

Is there a way to disable syntax error highlighting for certain file extensions in Visual Studio 2013/2015?

While I'm in the midst of working on JSX files in React, those wiggly lines keep catching my attention and distracting me. I've been trying to figure out if there's a way to turn off the syntax checker without relying on tools like resharpe ...

Invalid Hook Call error in Material UI framework

I am currently using a blog template, but I would like to switch to Material UI. However, every time I add the useStyles-Hook, I encounter an invalid hook error. Does anyone have a solution for implementing Material UI without facing this error? Thank you ...

When using Angular2, I have found that I am unable to extract the body data from JSONP responses. However, I have discovered that this issue

Initially, I developed the SERVER using spring-boot framework. The code for this looks like: public class App { @RequestMapping("/") @ResponseBody String home(HttpServletRequest request) { String aa=request.getParameter("callback"); System.out.pri ...

The curious case of ReactJs/NextJs useEffect(): Unveiling its mysterious double invocation

My custom useEffect() hook is consistently executed twice. It relies on two dependencies: reinitializePage (which triggers the useEffect when set to true) and corporateContext (which updates the component when the context changes). const [reinitializePage, ...

Issue with useState in Next.js when fetching data from an API

When attempting to retrieve data from the API, I am receiving a response. However, when trying to set the data to useState using the setAccessData function, the data is not being accessed properly. Despite trying multiple methods, the data continues to sho ...

Challenges when working with AJAX/jQuery in terms of fetching JSON data and setting dataType

I am currently facing a challenge with my practice of AJAX/jQuery coding. Despite my efforts to learn and improve, I find the concepts of jQuery and AJAX quite perplexing. Specifically, I am struggling to understand dataTypes and how to manage different ty ...

Managing Events for a Menu Item That Cannot Be Selected

As I work on developing a form using Material UI React, my focus is on logging exercise sets to the database. To allow users to select the exercise they wish to log, I have incorporated a list of Material UI Menu Item components as options. Additionally, I ...

The list is failing to display

The issue encountered: Objects are not valid as a React child. If you intended to render a collection of children, consider using an array instead. Here 'res' contains nested arrays in JSON format, so I utilized _.forEach to extract it. The st ...

Finalizing an item's status

I am quite puzzled about the workings of closures in this particular code snippet: function Spy(target, method) { var result = {count: 0}, oldFn = target[method]; target[method] = function(input) { result.count++; return ol ...

How to Delete Multiple Rows from an Angular 4 Table

I have successfully figured out how to remove a single row from a table using splice method. Now, I am looking to extend this functionality to remove multiple rows at once. html <tr *ngFor="let member of members; let i = index;"> <td> ...

What is causing this error/bug to show up in Angular?

I encountered an error while working on my Angular project that incorporates both front-end and back-end development with Python Flask. Even though the page updates correctly, a database-related error is being displayed in the console. Below are the snippe ...

Is it possible to alter the parent scope from an AngularJS directive?

I am currently in the process of constructing my initial Angular application, but I am encountering some difficulties while attempting to achieve a specific functionality. The issue revolves around a video container which is supposed to remain hidden until ...

Uploading Boolean Values from Switch Input (React/Typescript)

I'm facing an issue while trying to post the state value of a switch input toggle control. Whenever I use the submitRecommendation() function through a button click, I encounter a JSON parse error: Cannot deserialize instance of `boolean` out of START ...

React's Conditional Rendering

Let's imagine having these initial conditions: this.state = {plans: [], phase: 'daybreak'} along with a dynamic JSON object fetched from an API containing various schedules that may change periodically, for example: plans = {"daybreak": " ...

How can I enhance MUI Badge content with a tooltip?

Looking for a way to add a tooltip to my MUI Badge component. I attempted wrapping the badge with a ToolTip component from MUI, however, the tooltip text appears when the children are hovered as well. I only want it to show when the Badge itself is hovere ...

What is the reason behind this Uncaught TypeError that is happening?

After converting my questionnaire to a PHP file and adding a validation script, I encountered an error: Uncaught TypeError: Cannot set property 'onClick' of null The error is pointing me to line 163 in my JavaScript file, where the function f ...