Issue arising from the rendering of items in a Navigation Bar

I'm encountering an issue where only the last item of the navbar is being rendered even though the data appears to be correct. I've tried hard coding the data into the components but the same error persists.https://i.sstatic.net/ccEuB.png , https://i.sstatic.net/lCJ3o.png.

//Navbar component
import React from "react";
import { Link } from "react-router-dom";
import Dropdown from "./Dropdown";
import "./style/navbar.css";

const Navbar: React.FC = () => {
    const npflDropdownItems = [
        "Home",
        "Fixtures",
        "Results",
        "Tables",
        "Transfer",
        "Stats",
        "Video",
        "Watch Live",
        "Clubs",
        "Players",
        "Awards",
    ];
    const aboutDropdownItems = ["Overview", "What We Do", "Careers", "Partners"];
    const moreDropdownItems = ["Managers", "Referee", "e-NPFL", "Contact Us"];

    console.log(npflDropdownItems, aboutDropdownItems, moreDropdownItems);

    return (
        <nav className="navbar">
            <div className="logo-container">
                <img className="logo" src="path-to-your-logo.png" alt="Logo" />
            </div>
            <div className="navbar-dropdown">
                <div className="navbar-item npfl">
                    Npfl
                    <Dropdown items={npflDropdownItems} />
                </div>
            </div>
            <div className="navbar-dropdown">
                <div className="navbar-item about">
                    About
                    <Dropdown items={aboutDropdownItems} />
                </div>
            </div>
            <div className="navbar-dropdown">
                <div className="navbar-item more">
                    More
                    <Dropdown items={moreDropdownItems} />
                </div>
            </div>
        </nav>
    );
};

export default Navbar;

//Dropdown component
import React from "react";
import { Link } from "react-router-dom";

interface DropdownProps {
  items: string[];
}

const Dropdown: React.FC<DropdownProps> = ({ items }) => {
  return (
    <div className="navbar-dropdown">
      {items.map((item, index) => (
        <div key={index} className="navbar-item-dropdown">
          {item}
        </div>
      ))}
    </div>
  );
};

export default Dropdown;
`

`/* Navbar.css */

.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #333;
  padding: 10px;
  color: #fff;
}

.logo-container {
  max-width: 100px;
}

.logo {
  width: 100%;
  height: auto;
}

.navbar-menu {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.navbar-item {
  margin-right: 10px;
  cursor: pointer;
  color: #fff;
  text-decoration: none;
}

.navbar-dropdown {
  position: relative;
  z-index: 2;
}

.navbar-item-dropdown {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  background-color: #333;
  padding: 10px;
  width: 150px;
  z-index: 1;
}

.navbar-dropdown:hover .navbar-item-dropdown {
  display: block;
}

.fantasy,
.more {
  cursor: pointer;
}

@media (max-width: 768px) {
  .navbar-menu {
    flex-direction: row;
    align-items: flex-start;
  }

  .navbar-item,
  .fantasy,
  .more {
    margin: 5px 0;
  }

  .navbar-dropdown .navbar-item {
    width: 100%;
  }
}
`

I expect all dropdown items to be displayed when clicked or hovered.

Thanks in advance

Answer №1

There seems to be a issue with the code where only the last navigation item is visible due to the use of position:absolute, causing all list items to overlap.

In the provided structure:

<div class="navbar-dropdown">
    <div class="navbar-item">
        <div class="navbar-dropdown">
            <div class="navbar-item-dropdown"></div>
        </div>
    </div>
</div>

I suggest modifying the class names such as changing navbar-dropdown to navbar-dropdown-container and adjusting styles accordingly.

//Navbar component

  return (
    <nav className="navbar">
      <div className="logo-container">
        <img className="logo" src="path-to-your-logo.png" alt="Logo" />
      </div>
      <div className="navbar-dropdown-container">
        <div className="navbar-item npfl">
          Npfl
          <Dropdown items={npflDropdownItems} />
        </div>
      </div>
      <div className="navbar-dropdown-container">
        <div className="navbar-item about">
          About
          <Dropdown items={aboutDropdownItems} />
        </div>
      </div>
      <div className="navbar-dropdown-container">
        <div className="navbar-item more">
          More
          <Dropdown items={moreDropdownItems} />
        </div>
      </div>
    </nav>
  );
//Navbar.css

// Adding styles to container
.navbar-dropdown-container {
  position: relative;
  top:0;
  z-index: 2;
}

// Hiding navbar-dropdown as display none instead of list items and setting its position as absolute
.navbar-dropdown{
  display:none;
  position: absolute;
}

// Adjusting some styles for items
.navbar-item-dropdown {
  background-color: #333;
  padding: 10px;
  width: 150px;
  z-index: 1;
}

// Changing hover effect to target container and adding styles
.navbar-dropdown-container:hover .navbar-dropdown {
  display: flex;
  flex-direction: column;
}

Answer №2

To begin, within the Dropdown component file, iterate through each item in the items array and convert it to an <li> element enclosed within either a <ul> or <ol>.

Additionally, make sure you have included display: none for your .navbar-item-dropdown class in the stylesheet.

Finally, if there are any errors present in the console log, please provide them when submitting your post.

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

Issues with Angular routing when using anchor links

Recently, I delved into the world of Angular and encountered an issue with routing not working correctly when using anchor tags. Here's the snippet of code causing trouble: (function(){ 'use strict'; angular.module("PHC",["ngRoute"] ...

Navigating through the material ui swipeable drawer is a mystery to me

Utilizing the Material UI swipeable drawer, it showcases a loading message while awaiting data from the server. https://i.stack.imgur.com/5eQ86.png Once the data is fetched, the drawer should open to the center of the page with the data displayed. https ...

Guide on clearing filters in Angular 4

I'm facing an issue where I have implemented multiple filters but resetting them is not working as expected. showOnlyMyRequest(){ this.requests = this.requests.filter(request => request.requestedBy === 'John Doe'); } showAllReques ...

How can you prevent the blur event from triggering in jQuery when the next element to receive focus is of a specific type?

I am currently developing a unique form UI where I am working on creating a custom select box replacement. The custom select control consists of a hidden select input within a div, along with anchor elements inside a span that mimic the options of the sele ...

Steps to include a new file in an array

Hello everyone, I'm new to using React and I'm having trouble adding a new file to an array. Below is the code snippet: const [mymy, setMymy] = useState([ { name: "riky", age: "15" }, { name: "budi", age: &q ...

create a fresh variable instead of appending it to the current object

I'm encountering an issue where a new array is supposed to be added on callback using props, but instead an empty variable is being added. Here's the code snippet: const [data, setData] = useState({ title: "", serviceId: "", serviceNa ...

Storing the input into an array and resetting the input is not functioning as intended

A unique situation arises with the controller in which data is fetched from a factory: .controller('ChatDetailCtrl', function($scope, $stateParams, Messages) { $scope.messages = Messages.get($stateParams.partnerId); $scope.send = function (i ...

"Applying a background color to a BorderPane element is successful when done inline, but encounters issues when

So I have a border pane and I am trying to set its background color to black using CSS. When I directly set the style inline, like this: borderPane.setStyle("-fx-background-color: black;"); it works perfectly fine. However, when I try to add a class s ...

Use CSS to create a fullscreen animation for an element

Is there a way to animate a div element so that it expands to fit the screen when clicked without specifying an initial position? I need the div to be able to adjust dynamically within the screen. .box { width: 80px; height: 80px; background: red; ...

Two variations of identical descriptions within a single div container

Is there a way for me to use an item, specifically an img, within the same div class but with different definitions? For example: section .row img { margin: 0 0 30px; width: 100%; border: 4px solid #18a00e; } section .row img { margin: 0 ...

Protractor - Not waiting for all elements to load before opening a popup window

Despite my efforts to ensure the stability of my test, there are instances where it does not wait for all elements within an integrated popup, resulting in failure. This particular test case is quite intricate as it involves nested if statements to execut ...

Is it possible to create a .d.ts file to easily share constants between JavaScript and TypeScript?

Currently, I am in the midst of a node.js project that involves both TypeScript and JavaScript due to historical reasons. My goal is to establish project-wide constants that can be utilized in both languages. Initially, I thought about creating a .js file ...

How to effectively manage errors in TypeScript using RxJS?

Exploring subscribe arguments in the official RxJS documentation has raised some interesting questions for me. For instance, what is the purpose of using error: (e: string) => { ... } to catch errors emitted by an observable? Despite this implementation ...

Problem with Jquery hover or mouse enter show/hide functionality

I am currently experimenting with displaying hidden text when the mouse enters a div and hiding it when it leaves. Here is the progress I've made on my JSFiddle: $(document).ready(function() { $(".image").mouseover(function(){ $(".hover").show ...

How can I efficiently retrieve a string echoed or returned from PHP using jQuery/Ajax?

Suppose I have the following code in my test.php file: echo 'Hello, world' I want to display that text within a div element. However, this method is not working for me and the alert message is not popping up. $.ajax({ type: "GET", url: ...

What is the process for incorporating Express.js variables into SQL queries?

Recently delving into the world of node.js, I've embarked on a journey to create a login and registration system using express.js, vanilla JS, CSS, HTML, and MySql. Within the following code lies the logic for routing and handling HTTP Post requests ...

Utilizing Jquery Validation to Remove a Class Upon Form Validation Success

In my current registration process, I have a multipart form where each subsequent form is displayed when the next button is pressed without fading effects. Initially, the button appears faded. Here's a simplified version of how I handle the first form ...

The cards in the Bootstrap layout stack on top of one another when viewed on a mobile screen

I have a "flipping card" and another bootstrap 4 card positioned side by side. However, when tested on a mobile screen using Chrome's dev console, the second card overlaps the flipping card and covers it completely. To provide some context, I've ...

Creating a donut chart sparkline in React using Highcharts - a step-by-step guide

I am looking to create a donut sparkline chart in my React application using Highcharts. Can anyone guide me on how to achieve this functionality within React? My goal is to generate a visualization like the following, illustrating the percentage of a cer ...

Issue with back button functionality when loading page with history.pushState in JavaScript

My current issue involves loading a page via ajax using history.pushState. The page loads successfully, but the back button does not work as expected. I have included my code below for reference: function processAjaxData(response, urlPath){ document.wr ...