Ant Design Library has this component pre-built, and it is very easy to integrate as well. Dropdown Component is used to provide a dropdown list, and it is used when the user has more than a few options to choose from. We can use the following approach in ReactJS to use the Ant Design Dropdown Component.
Dropdown Props:
- arrow: It is used to indicate whether the dropdown arrow should be visible or not.
- disabled: It is used to indicate whether the dropdown menu is disabled or not.
- getPopupContainer: It is used to set the container of the dropdown menu.
- overlay: It is the dropdown menu.
- overlayClassName: It is used to pass the class name of the dropdown root element.
- overlayStyle: It is used for the style of the dropdown root element.
- placement: It is used for the placement of the popup menu.
- trigger: It is the trigger mode that executes the dropdown action.
- visible: It is used to indicate whether the dropdown menu is currently visible or not.
- onVisibleChange: It is a callback function that is called when the visible state is changed.
Dropdown.Button Props:
- buttonsRender: It is used for the custom buttons inside Dropdown.Button.
- disabled: It is used to indicate whether the dropdown menu is disabled or not
- icon: It is used to pass the Icon which appears on the right.
- overlay: It is the dropdown menu.
- placement: It is used for the placement of the popup menu.
- size: It is used to denote the size of the button.
- trigger: It is the trigger mode that executes the dropdown action.
- type: It is used to denote the type of the button.
- visible: It is used to indicate whether the dropdown menu is currently or not.
- onClick: It is a callback function that is called when you click the button on the left.
- onVisibleChange: It is a callback function that is called when the visible state is changed.
Creating React Application And Installing Module:
-
Step 1: Create a React application using the following command:
npx create-react-app foldername
-
Step 2: After creating your project folder i.e. foldername, move to it using the following command:
cd foldername
-
Step 3: After creating the ReactJS application, Install the required module using the following command:
npm install antd
Project Structure: It will look like the following.
Example: Now write down the following code in the App.js file. Here, App is our default component where we have written our code.
App.js
import React from 'react' import "antd/dist/antd.css" ; import { Menu, Dropdown } from 'antd' ; export default function App() { return ( <div style={{ display: 'block' , width: 700, padding: 30 }}> <h4>ReactJS Ant-Design Dropdown Component</h4> <> <Dropdown overlay={( <Menu> <Menu.Item key= "0" > Menu Item One </Menu.Item> <Menu.Item key= "1" > Menu Item Two </Menu.Item> <Menu.Item key= "1" > Menu Item Three </Menu.Item> </Menu> )} trigger={[ 'click' ]}> <a className= "ant-dropdown-link" onClick={e => e.preventDefault()}> Open Dropdown </a> </Dropdown> </> </div> ); } |
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:
Reference: https://ant.design/components/dropdown/