Material UI is a popular React UI library that provides a set of pre-designed components following the Material Design guidelines. One of the key components in Material UI is the AppBar component, which provides a flexible and customizable navigation bar for your web applications. In this article, we will explore how to use the AppBar component in Material UI to create a navigation bar with ease.
The App Bar displays information and actions relating to the current screen. Material UI for React has this component available for us and it is very easy to integrate. We can use the AppBar Component in ReactJS using the following approach.
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 MUI modules using the following command:
npm install @mui/material npm install @mui/icons-material
Project Structure: It will look like the following.
Example 1: In this example, we will create a basic app bar application, that will have a menu icon and some text.
Now write down the following code in the App.js file. Here, App is our default component where we have written our code.
Filename: App.js:
Javascript
import * as React from 'react' ; import AppBar from '@mui/material/AppBar' ; import Toolbar from '@mui/material/Toolbar' ; import Typography from '@mui/material/Typography' ; import Button from '@mui/material/Button' ; import IconButton from '@mui/material/IconButton' ; import MenuIcon from '@mui/icons-material/Menu' ; export default function ButtonAppBar() { return ( <AppBar> <Toolbar> <IconButton size= "large" edge= "start" color= "inherit" aria-label= "menu" sx={{ mr: 2 }} > <MenuIcon /> </IconButton> <Typography variant= "h6" component= "div" sx={{ flexGrow: 1 }}> Geeksforneveropen </Typography> <Button color= "inherit" >Logout</Button> </Toolbar> </AppBar> ); } |
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.
Example 2: In this example, we will create a simple app bar application with a toggle-able menu.
Filename: App.js
Javascript
import * as React from 'react' ; import AppBar from '@mui/material/AppBar' ; import Box from '@mui/material/Box' ; import Toolbar from '@mui/material/Toolbar' ; import Typography from '@mui/material/Typography' ; import IconButton from '@mui/material/IconButton' ; import MenuIcon from '@mui/icons-material/Menu' ; import Switch from '@mui/material/Switch' ; import FormControlLabel from '@mui/material/FormControlLabel' ; import FormGroup from '@mui/material/FormGroup' ; export default function MenuAppBar() { const [auth, setAuth] = React.useState( true ); const handleChange = (event) => { setAuth(event.target.checked); }; return ( <Box sx={{ flexGrow: 1 }}> <FormGroup> <FormControlLabel control={ <Switch checked={auth} onChange={handleChange} aria-label= "login switch" /> } label={auth ? 'Hide Menu' : 'Show Menu' } /> </FormGroup> <AppBar position= "static" > <Toolbar> <IconButton size= "large" edge= "start" color= "inherit" aria-label= "menu" sx={{ mr: 2 }} > <MenuIcon /> </IconButton> <Typography variant= "h6" component= "div" sx={{ flexGrow: 1 }}> Geeksforneveropen </Typography> {auth && ( <div style={{ display: 'flex' , gap: '10px' }}> <Typography>Contact US</Typography> <Typography>About US</Typography> </div> )} </Toolbar> </AppBar> </Box> ); } |
Steps to run the application:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output.
Reference: https://mui.com/material-ui/react-app-bar/