This article will teach you how to use the AppBar API offered by React MUI. MUI stands for Material UI. It is a framework that provides Component APIs that you can use to make your website more attractive. Let us take a look at the AppBar API.
The React MUI AppBar API is a Component API offered by MUI. These AppBars are similar to Navigation bars and you will mostly observe them, at the top of a webpage. The AppBar generally contains elements such as Menus, Dropdown lists, Login Buttons, Profile icons, and more. You will understand better once we implement the API.
Syntax:
<AppBar>
{/* Components that you want to render on the AppBar */}
</AppBar>
Props: The props available for this component are as follows
- classes: It is used to extend the CSS applied to the component. Through classes, we can apply different CSS Classes to the component, to make it more attractive.
- color: It is used to apply a color to our AppBar. We can apply a default as well as a custom color.
- enableColorOnDark: It is used to apply a darker shade of color to our AppBar.
- position: This is the same as the positioning in normal CSS. For example: if we set the positioning prop to fixed, then our AppBar will be fixed at the top of the page, and won't be visible once you scroll down the page considerably.
- sx: With this prop, we can write our custom CSS and apply to the props instead of their default values. We can also write additional CSS that we want to apply to our AppBar.
Let's understand with examples.
Step 1: Create a folder called appbar-react. Open your command prompt and navigate to the appbar-react folder. Now type in the following command.
npx create-react-app
Step 2: Again, in the same folder, open the command prompt and type in the following command.
npm install @mui/material @emotion/react @emotion/styled
Project Structure: Once the installation is complete, you should have the necessary starter files. Your folder structure should look something like this.
Folder Structure
Now, inside the src folder, create a new folder called components. Inside the components folder, create two files namely AppBarNavbarComponent.js and AppbarToggleComponent.js. Your updated folder structure should look like this.
Updated Folder Structure
Example 1: In this example, we will create a simple navbar inside our AppBar component.
AppBarNavbarComponent.js
JavaScript
import { AppBar } from '@mui/material';
import React from 'react';
const AppBarNavbarComponent = () => {
return (
<>
<AppBar>
<ul id="nav-list">
<li>Home</li>
<li>About</li>
<li>Learn More</li>
<li>Contact Us</li>
</ul>
</AppBar>
</>
)
}
export default AppBarNavbarComponent
Now we will add some CSS for the above component. The CSS is given below. Just inside your src folder, open App.css and type the following code.
App.css
CSS
#nav-list {
display: flex;
}
#nav-list li {
list-style-type: none;
padding: 20px;
border-radius: 10px;
transition: background 1s;
}
#nav-list li:hover {
cursor: pointer;
background-color: rgb(49 49 217);
font-weight: 600;
}
.theme {
padding: 13px;
margin: 13px;
border: none;
border-radius: 10px;
font-weight: 600;
}
#normal {
background-color: #fff;
}
#dark {
background-color: #000;
color: #fff;
}
.theme:hover {
cursor: pointer;
}
Now open your App.js inside the src folder. In App.js, we are simply importing the AppBarNavbarComponent and rendering it inside the return statement.
App.js
JavaScript
import './App.css';
import AppBarNavbarComponent from
'./components/AppBarNavbarComponent';
function App() {
return (
<>
<AppBarNavbarComponent />
</>
);
}
export default App;
Step to run the application: Open you command prompt in the same folder, and type in the following command.
npm start
Output:
Output for AppBarNavbarComponent
Example 2: In this example, we will create two buttons that change the color of our AppBar. We will toggle to do the switch between normal and dark mode.
AppBarToggleComponent.js
JavaScript
import { AppBar } from '@mui/material'
import React, { useState } from 'react'
const AppBarToggleComponent = () => {
const [theme, setTheme] = useState('transparent');
const handleChange = () => {
setTheme('primary');
}
const handleReset = () => {
setTheme('transparent');
}
return (
<>
<AppBar color={theme}>
<div>
<button className="theme" id="normal"
onClick={handleReset}>
Transparent Theme
</button>
<button className="theme" id="dark"
onClick={handleChange}>
Primary Theme
</button>
</div>
</AppBar>
</>
)
}
export default AppBarToggleComponent
Now, open App.js inside the src folder. In App.js, we are simply importing the AppBarToggleComponent and rendering it inside the return statement. Also, we are importing the App.css file. The same CSS file is used for both the examples.
App.js
JavaScript
import './App.css';
import AppBarToggleComponent from
'./components/AppBarToggleComponent';
function App() {
return (
<>
<AppBarToggleComponent />
</>
);
}
export default App;
Step to run the application: Open your command prompt and type in the following command.
npm start
Output:
AppBarToggleComponent
Reference: https://mui.com/material-ui/api/app-bar/
Similar Reads
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
React Fundamentals
React IntroductionReactJS is a component-based JavaScript library used to build dynamic and interactive user interfaces. It simplifies the creation of single-page applications (SPAs) with a focus on performance and maintainability. "Hello, World!" Program in ReactJavaScriptimport React from 'react'; function App() {
6 min read
React Environment SetupTo run any React application, we need to first setup a ReactJS Development Environment. In this article, we will show you a step-by-step guide to installing and configuring a working React development environment.Pre-requisite:We must have Nodejs installed on our PC. So, the very first step will be
3 min read
React JS ReactDOMReactDOM is a core React package that provides DOM-specific methods to interact with and manipulate the Document Object Model (DOM), enabling efficient rendering and management of web page elements. ReactDOM is used for: Rendering Components: Displays React components in the DOM.DOM Manipulation: Al
2 min read
React JSXJSX stands for JavaScript XML, and it is a special syntax used in React to simplify building user interfaces. JSX allows you to write HTML-like code directly inside JavaScript, enabling you to create UI components more efficiently. Although JSX looks like regular HTML, itâs actually a syntax extensi
5 min read
ReactJS Rendering ElementsIn this article we will learn about rendering elements in ReactJS, updating the rendered elements and will also discuss about how efficiently the elements are rendered.What are React Elements?React elements are the smallest building blocks of a React application. They are different from DOM elements
3 min read
React ListsIn lists, React makes it easier to render multiple elements dynamically from arrays or objects, ensuring efficient and reusable code. Since nearly 85% of React projects involve displaying data collectionsâlike user profiles, product catalogs, or tasksâunderstanding how to work with lists.To render a
4 min read
React FormsIn React, forms are used to take input from users, like text, numbers, or selections. They work just like HTML forms but are often controlled by React state so you can easily track and update the input values.Example:JavaScriptimport React, { useState } from 'react'; function MyForm() { const [name,
4 min read
ReactJS KeysA key serves as a unique identifier in React, helping to track which items in a list have changed, been updated, or removed. It is particularly useful when dynamically creating components or when users modify the list. When rendering a list, you need to assign a unique key prop to each element in th
4 min read
Components in React
React Lifecycle In React, the lifecycle refers to the various stages a component goes through. These stages allow developers to run specific code at key moments, such as when the component is created, updated, or removed. By understanding the React lifecycle, you can better manage resources, side effects, and perfo
7 min read
React Hooks
Routing in React
Advanced React Concepts
React Projects