Skip to main content

Command Palette

Search for a command to run...

Display navbar item with Map() in React

Updated
1 min read
Display navbar item with Map() in React
R

Hello, I am Rafi kadir and I am from Bangladesh. Taking Web Development as a profession not only fulfills my pocket but also my heart because it has been my passion since my early teenage.

1. Create Items in your navbar.js file

import React from 'react';

const navItems = [
    {
        name: 'Home',
        href: '#home'
    },
    {
        name: 'About',
        href: '#about'
    },
    {
        name: 'Contact',
        href: '#contact'
    }
    ,
    {
        name: 'Hi',
        href: '#contact'
    }
]

2. I am using the function component

const Navbar = () => {
    return (    
        <div class="collapse navbar-collapse" id="navbarSupportedContent">
               <ul class="navbar-nav ms-auto mb-2 mb-lg-0">
                      {
                          navItems.map(navItem => {
                                 return <li class="nav-item">
                                                <a class="nav-link" href={navItem.href}>{navItem.name}           
                                                </a>
                                      </li>
                       })
                          }
               </ul>
         </div> 
    );
};

export default Navbar;
  1. mapping navItems and the map will keep all data in navItem
  2. Then I can call it where I need <a class="nav-link" href={navItem.href}> {navItem.name} </a>
  3. For example I need nameso that I declare navItem.name
D

Thanks for explanation Rafi kadir 👍

1
R

Welcome

React

Part 4 of 6

In this series, I will share React Tips, Tutorials, Project and Resources.

Up next

How to display Icon in React Bootstrap Tab with text

How to add icon in React Bootstrap Tabs

More from this blog

R

Rafi Kadir

27 posts

React Frontend Developer. I write about Frontend development, CSS, React and Javascript.