How to Create a Responsive Horizontal Menu with Antd Component Library

Jacob Narayan
Bits and Pieces
Published in
4 min readMar 7, 2023

--

In this tutorial, we will learn how to create a responsive horizontal menu using the Antd component library. Antd is a React UI library that provides a range of high-quality components out of the box. It is incredibly easy to use and provides a clean and modern look to your application. Let’s get started!

1. Prerequisites

Before beginning this tutorial, you should have React installed and have basic familiarity with the Antd component library. You should also make sure you have an up-to-date version of Node.js installed on your system.

2. Setup

In order to get started, you should create a new React application by running the following command in your terminal:

npx create-react-app my-app

Once your application has been created, you should navigate into the directory and install the Antd library by running the command:

npm install antd

3. Create the Menu Component

Once you have the Antd library installed, you can begin creating the menu component by creating a new file called Menu.js in your src directory. In this file, we will import the Menu component from Antd and define our menu items.

import React from 'react'; 
import { Menu } from 'antd';

const MenuItems = [
{ name: 'Home', link: '/home' },
{ name: 'About', link: '/about' },
{ name: 'Contact', link: '/contact' }
];

const MyMenu = () => {

return (
<Menu mode="horizontal">

{MenuItems.map(item => (

<Menu.Item key={item.name}>

<a href={item.link}>{item.name}</a>

</Menu.Item>

))}

</Menu>

);

};

export default MyMenu;

In this code snippet, we are importing the Menu component from Antd and creating an array of menu items that we want to display in our menu. We then define our MyMenu component which uses the Menu component from Antd in order to render our menu items. We are also setting the mode prop of the Menu component to horizontal, which will ensure that our menu is displayed horizontally across the page. Finally, we export our MyMenu component so that it can be used in other parts of our application.

4. Add the Menu Component to App.js

Now that we have our MyMenu component defined, we need to add it to our main App component in order for it to be displayed on our page. To do this, open up your App.js file and import the MyMenu component at the top of the file:

import MyMenu from './components/menu';

Next, we need to add the MyMenu component to our App component's render method as shown below:

class App extends Component {  

render() {

return (

<div className="App">

<MyMenu />

{/* Other components here */}

</div>

);

}
}

Now if you save your changes and run your application, you should see your newly created horizontal menu displayed on your page! Congratulations! You have now successfully created a responsive horizontal menu using the Antd component library!

💡 Note: Once you’ve built this responsive horizonal menu, it might be a good idea to isolate and extract this component into packages, so you can use an open-source toolchain like Bit to publish, version, and reuse it across all of your projects with a simple npm i @bit/your-username/MyMenu. Find out more here, and here.

Thank you for reading. If you liked this blog, check out my personal blog for more content like this.

Build React Apps with reusable components, just like Lego

Bit’s open-source tool help 250,000+ devs to build apps with components.

Turn any UI, feature, or page into a reusable component — and share it across your applications. It’s easier to collaborate and build faster.

Learn more

Split apps into components to make app development easier, and enjoy the best experience for the workflows you want:

Micro-Frontends

Design System

Code-Sharing and reuse

Monorepo

--

--

I'm a full-stack software developer who has worked for several Fortune 500 companies. I specialize in .NET, Angular, and React Development.