How to create a bottom navigation in Material-UI react
YouTube video:
I have published one YouTube video, you can watch it here:
Components to use:
We need to use two components for showing a bottom navigation: BottomNavigation and BottomNavigationAction. Both are defined in @material-ui/core.
import { BottomNavigation, BottomNavigationAction } from "@material-ui/core";
Example program:
Let's take a look at the below program:
import { BottomNavigation, BottomNavigationAction } from "@material-ui/core";import {AccessAlarmOutlined,AccountBalanceOutlined,AdbOutlined,AddOutlined,} from "@material-ui/icons";import { useState } from "react";function App() {const [selected, setSelected] = useState(0);return (<BottomNavigationvalue={selected}onChange={(value, newValue) => {setSelected(newValue);}}style={{ width: "50%" }}><BottomNavigationAction label="First" icon={<AccessAlarmOutlined />} /><BottomNavigationActionlabel="Second"icon={<AccountBalanceOutlined />}/><BottomNavigationAction label="Third" icon={<AdbOutlined />} /><BottomNavigationAction label="Fourth" icon={<AddOutlined />} /></BottomNavigation>);}export default App;
- It will create one BottomNavigation with four BottomNavigationAction.
- label and icon are used to show in each action.
- selected is used to define the current selected menu item.
- On clicking any item, it will call the onChange method, which changes the selected value.
Show the labels always:
We can show the labels with the action buttons always. We need to use the showLabels props for that:
<BottomNavigationvalue={selected}onChange={(value, newValue) => {setSelected(newValue);}}style={{ width: "50%" }}showLabels>......