Material UI | ReactJS AppBar
Last Updated :
27 Nov, 2020
Material-UI is a library that provides React components for easy and fast web development. We can easily put together really aesthetic and functional components and make it work according to our use as all the components are configurable. This saves a lot of time as we don't have to struggle with CSS to make things presentable. Material-UI components work in isolation. They are self-supporting, and will only inject the styles they need to display.
You’ll learn how to set up and use Material-UI in your React web application as we build a demo " Sign In " application.
As you can see the user interface consists of material design elements. We’ll use Material-UI components to display a Sign In form to the user. The application consists of an App Bar on top which contains the title of the application, two text field which is used to input email and password and a Button to Sign In.
Generating The React Project: First, we need to create a new React project. This can be done by using the create-react-app script in the following way:
npx create-react-app signin-material-ui
After executing this command a new project directory signin-material-UI is available. Change into the newly created project folder and you’ll find the React starter project.
Start your React project by:
npm start
Installing Material-UI Library & Dependencies: To be able to use Material-UI components we have to make sure that we have installed it on our project which can be done by:
npm install @material-ui/core
Implementing The Sample Application: Deleting The Default Content, before starting building our project we need to delete the default contents of the project responsible for the start screen by:
- Select all files in the src folder and delete them.
- Create a new file index.js in the src folder.
Now we can add our own code to the index.js file.
Components we will be using :
Component Name | Props Used | Description |
---|
AppBar | position | The positioning type. |
---|
Toolbar | - | Toolbar |
---|
Button | variant | The variant to use. |
---|
color | The color of the component. |
fullwidth | If true, the button will take up the full width of its container. |
Text Field | variant | The variant to use. |
---|
margin | If dense or normal, will adjust vertical spacing of this and contained components. |
required | If true, the label is displayed as required and the input element` will be required. |
fullWidth | If true, the input will take up the full width of its container. |
id | The id of the input element. Use this prop to make label and helperText accessible for screen readers. |
label | The label content. |
name | Name attribute of the input element. |
type | Type of the input element. It should be a valid HTML5 input type. |
autoComplete | This prop helps users to fill forms faster, especially on mobile devices. |
FormControlLabel | control | A control element. For instance, it can be a Radio, a Switch or a Checkbox. |
---|
label | The text to be used in an enclosing label element. |
Checkbox | value | The value of the component. The browser uses "on" as the default value. |
---|
color | The color of the component. |
Link | href | Link address. |
---|
variant | Applies the theme typography styles. |
Grid | container | If true, the component will have the flex container behavior. You should be wrapping items in a container. |
---|
item | If true, the component will have the flex item behavior. You should be wrapping items in a container. |
Typography | variant | Applies the theme typography styles. |
---|
Container | component | The component used for the root node. Either a string to use an HTML element or a component. |
---|
maxWidth | Determine the max-width of the container. The container width grows with the size of the screen. Set to false to disable maxWidth. |
Importing Above Components: We have to import these components before we can make use of them in our project by writing import statements for every component in our index.js file along with necessary import statements of React and ReactDOM.
JavaScript
import React from "react";
import ReactDOM from "react-dom";
import AppBar from "@material-ui/core/AppBar";
import Toolbar from "@material-ui/core/Toolbar";
import Button from "@material-ui/core/Button";
import TextField from "@material-ui/core/TextField";
import FormControlLabel from "@material-ui/core/FormControlLabel";
import Checkbox from "@material-ui/core/Checkbox";
import Link from "@material-ui/core/Link";
import Grid from "@material-ui/core/Grid";
import Typography from "@material-ui/core/Typography";
import Container from "@material-ui/core/Container";
Creating Signing function: First, we will create a container element inside the SignIn function which will be used in wrapping all the components.
JavaScript
function SignIn(){
return(
<Container component="main" maxWidth="xs">
<div>
.
.
.
</div>
</Container>
)
}
ReactDOM.render(<SignIn />, document.getElementById("root"));
Creating an App Bar component:
JavaScript
<AppBar position="static">
<Toolbar>
<Typography variant="h6">Sign In</Typography>
</Toolbar>
</AppBar>
Output: We did not included any component, there is only a title.
Creating Sign In form component: The form will contain two text fields each for email and password, a remember me checkbox, a Sign In button, and some links.
JavaScript
<form noValidate>
// Email Textfield
<TextField
variant="outlined"
margin="normal"
required
fullWidth
id="email"
label="Email Address"
name="email"
autoComplete="email"
autoFocus
/>
// Password Textfield
<TextField
variant="outlined"
margin="normal"
required
fullWidth
name="password"
label="Password"
type="password"
id="password"
autoComplete="current-password"
/>
// Remember Me Checkbox
<FormControlLabel
control={<Checkbox value="remember"
color="primary" />}
label="Remember me"
/>
// Sign In button
<Button type="submit"
fullWidth variant="contained"
color="primary">
Sign In
</Button>
<Grid container>
<Grid item xs>
// Forgot Password link
<Link href="#" variant="body2">
Forgot password?
</Link>
</Grid>
<Grid item>
// Sign Up Link
<Link href="#" variant="body2">
{"Don't have an account? Sign Up"}
</Link>
</Grid>
</Grid>
</form>
Output: By making this component, we have completed our project.

Complete Code: This is index.js if you clear the src folder and created a single indes.js file.
JavaScript
import React from "react";
import ReactDOM from "react-dom";
import AppBar from "@material-ui/core/AppBar";
import Toolbar from "@material-ui/core/Toolbar";
import Button from "@material-ui/core/Button";
import TextField from "@material-ui/core/TextField";
import FormControlLabel from "@material-ui/core/FormControlLabel";
import Checkbox from "@material-ui/core/Checkbox";
import Link from "@material-ui/core/Link";
import Grid from "@material-ui/core/Grid";
import Typography from "@material-ui/core/Typography";
import Container from "@material-ui/core/Container";
function SignIn() {
return (
<Container component="main" maxWidth="xs">
<div>
<AppBar position="static">
<Toolbar>
<Typography variant="h6">
Sign In
</Typography>
</Toolbar>
</AppBar>
<form noValidate>
<TextField
variant="outlined"
margin="normal"
required
fullWidth
id="email"
label="Email Address"
name="email"
autoComplete="email"
autoFocus
/>
<TextField
variant="outlined"
margin="normal"
required
fullWidth
name="password"
label="Password"
type="password"
id="password"
autoComplete="current-password"
/>
<FormControlLabel
control={<Checkbox value="remember"
color="primary" />}
label="Remember me"
/>
<Button type="submit"
fullWidth variant="contained"
color="primary">
Sign In
</Button>
<Grid container>
<Grid item xs>
<Link href="#" variant="body2">
Forgot password?
</Link>
</Grid>
<Grid item>
<Link href="#" variant="body2">
{"Don't have an account? Sign Up"}
</Link>
</Grid>
</Grid>
</form>
</div>
</Container>
);
}
ReactDOM.render(<SignIn />, document.getElementById("root"));
Output:
Similar Reads
React Material UI
MUI or Material UI is an open-source React Components library that is based on Google's Material Design and provides the predefined UI components for React.Material UI is a UI library that provides predefined react components implementing Google's Material Design. Material UI is a design language bu
5 min read
React MUI AppBar API
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 M
4 min read
React Suite Nav Appearance
A React suite is a library of React components, sensible UI design, and a friendly development experience. It is supported in all major browsers. It provides pre-built components of React which can be used easily in any web application. In this article, we'll learn about React suite nav appearance.
3 min read
React Chakra UI Other
Chakra UI is a useful tool that allows users to design appealing and practical websites and applications in a quick and easy way. It provides a variety of ready-to-use components that can be personalized and adjusted to meet different project needs, thus streamlining web development and making it mo
6 min read
Material UI List using React.js
Lists are a continuous group of text or images. They are composed of items containing primary and supplemental actions, which are represented by icons and text. Material UI for React has this component available for us, and it is very easy to integrate. We can use the List Component in ReactJS using
5 min read
React Chakra UI Other Portal
Chakra UI Other Portal component by Chakra UI allows and enables users to render the components outside their parent hierarchy, this is useful for rendering efficiently React Applications. We can use this component where we need to be visually positioned in the DOM outside its parent. In this articl
3 min read
Material UI Container
Material-UI is a user interface framework that provides pre-defined and customizable React components for faster and easy web development, these Material-UI components are based on top of Material Design by Google. In this article letâs discuss the Typography component in the Material-UI library.Con
2 min read
React MUI Material Icons
MUI or Material-UI, it's a React component library. It enables you to build your own design system and develop React applications faster. It is basically a design language that was developed by Google in 2014. It uses more Design and Animation, grid-system, and provides shadows and lightning effects
3 min read
React Chakra UI Media and icons
React Chakra UI Media and Icons component is tailored for managing media content and displaying icons and images. Chakra UI provides a set of commonly used interface icons you can use in your websites or apps. It helps the user provide a better user experience by identifying functionalities by looki
5 min read
React Suite Navbar Props
React Suite is a front-end library designed for the middle platform and back-end products. The React Suite Navbar component acts as a navigator at the top of a website and it allows the user to provide an easy way to navigate. Navbar props: as: It denotes the element type of the component. It is 'di
4 min read