How to integrate Material UI library in React apps

Published on May 24, 2020

8 min read


Material design was introduced by Google around 2014 as a design language and it still shares some popularity among web and mobile applications. One of the common ways to integrate and use this design system in React apps is through MaterialUI library.


In this post, let us take a look at how to integrate a material library that is available as an npm module and consists of built React components that you can use to build apps. You are going to learn from scratch on how to install and configure this library as well as build a small demo app.




Before you begin this tutorial you are going to need the following:

  • a Web browser such as Chrome, Firefox and so on
  • Node.js version above 12.x.x installed on your local machine
  • JavaScript/ES6 basics
  • React basics
  • npm/yarn install
  • either create-react-app globally installed or access via npx

Create a new React app


To start, you are going to need to setup a React project. Let's use npx to do so. Execute the following command in a terminal window.

npx create-react-app reactmaterial

By using the npx command (a part of the Node Package Manager (npm)) you execute create-react-app without the need to download and install the package globally.

When the new project is done scaffolding with the default dependencies such as React library and React DOM library installed.

Change in the new directory and you are going to be welcomed by the following structure of files and folders.


To verify that the React app is working, (you can totally skip the step at this time), run yarn start from the terminal window and open the default React app running at http://localhost:3000 in a browser window.


Install Material UI library


The next step is to install the Material-UI library and integrate it to work with the current React app. The first is to install the core dependency of this library.

yarn add @material-ui/core

Material-UI was designed with the Roboto font in mind. If you wish to use it, you can follow the instructions. Go back to the terminal window and install the typeface package of the font. Also note that, the following steps to install this font are optional.

yarn add typeface-roboto

Then go to the entry point of your React app (preferably, ./src/index.js file) and import the font.

1// rest of the import statements
2import 'typeface-roboto';

Alternatively, if you do not wish to install the above npm module for the font, you can also use the CDN version of the font and read about it here.

Using AppBar to create a custom navigation bar


In this section let us build a navigation bar. Create a new file called ./src/components/Navbar.js and start by importing the components AppBar, ToolBar, and Typography from the MaterialUI library.

The AppBar component is used to display branding, screen titles, and navigation of the web app. That is what you are going to use it for. The ToolBar component is wrapper where you can place your components horizontally. The Typography component applies the Material UI theme that is available by default.

1import React from 'react';
2import { AppBar, Toolbar, Typography } from '@material-ui/core';

Next, export a function component called Navbar with the following JSX.

1export default function Navbar() {
2 return (
3 <AppBar>
4 <Toolbar>
5 <Typography variant="h6">React and Material-UI App</Typography>
6 </Toolbar>
7 </AppBar>
8 );

In the code snippet above, notice the variant prop on the Typography component. It uses the variant mapping to properly associate a UI element with an HTML semantic element (such as h6 in the code snippet).

Now, to see it in action, import the Navbar component in the App.js file.

1import React from 'react';
2import Navbar from './components/Navbar';
4function App() {
5 return (
6 <div>
7 <Navbar />
8 </div>
9 );
12export default App;

You are going to get the following result after this step.


Implementing a Paper component


Two components that are going to be used in this section in order to layout a view for the items of the list are called Grid and Paper.

Material Design’s responsive UI is based on a 12-column grid layout. The Grid component helps you implement this layout system and then provide the values or the number of grids you want to display. A basic grid might look like below:


Material UI uses CSS's Flexbox to manage layout alignments and sizing of the grid columns.

The other component Paper actually displays the physical properties of a paper on the screen. It resembles a flat, texture of a sheet of paper, and using a prop called elevation you can manipulate its default display behavior. Do note that, this component does need an initial width and height.

Create a new component file called ./src/components/List.js which is going to be used as a reusable component later. This presentational component is going to display a Paper component inside Grid with custom styles.

1import React from 'react';
2import { Grid, Paper, makeStyles } from '@material-ui/core';
4const useStyles = makeStyles(theme => ({
5 root: {
6 marginTop: 100,
7 flexGrow: 1
8 },
9 paper: {
10 height: 220,
11 width: 340,
12 backgroundColor: '#ebebeb'
13 },
14 avatarImage: {
15 width: 200,
16 height: 200,
17 borderRadius: 100
18 }
21export default function List() {
22 const classes = useStyles();
24 return (
25 <Grid container spacing={2}>
26 <Grid item className={classes.root} xs={12}>
27 <Grid container justify="center" spacing={2}>
28 <Grid key={1} item>
29 <Paper className={classes.paper} elevation={2} />
30 </Grid>
31 </Grid>
32 </Grid>
33 </Grid>
34 );

The makeStyles comes from the Material UI API. It links a style sheet with a function component and returns a hook that you use inside the component.

You can modify the App.js file to include the List component.

1// rest of the import statements
2import List from './components/List';
4function App() {
5 return (
6 <div>
7 <Navbar />
8 <List />
9 </div>
10 );

Here is the result you are going to get after this step.


Fetching random user data


To display data inside List component, let us use API. In the App component the data is going to be fetched on the initial render using useEffect hook. The useState hook is used to store the fetched data by defining a state variable whose initial value is an empty array.

To start, first, import the hooks from the react library.

1import React, { useState, useEffect } from 'react';

After that create a state variable called data inside the App component. Along with the data variable, define two other state variables, isLoading to track the whether app is in loading mode (that is, the data is being fetched from the API) and error to track if there is an error while fetching the data. The loading state of the React app is going to be true by default.

1function App() {
2 const [data, setData] = useState([]);
3 const [isLoading, setIsLoading] = useState(true);
4 const [error, setError] = useState('');
6 //rest of the code

The useEffect hook is going to have a callback that is going to fetch the data using JavaScript's fetch API.

1useEffect(() => {
2 fetch('')
3 .then(res => res.json())
4 .then(
5 result => {
6 setIsLoading(false);
7 setData(result.results);
8 },
9 error => {
10 setIsLoading(false);
11 setError(error);
12 }
13 );
14}, []);

You can conditionally render the JSX based on the state variables.

1if (isLoading) {
2 return <div>Loading ...</div>;
3} else if (error) {
4 return <div>There is an error fetching data.</div>;
5} else {
6 return (
7 <div>
8 <Navbar />
9 <List />
10 </div>
11 );

Now, when you refresh the React app, you are going to see the JSX being rendered that is related to the loading state of the app, for split seconds.


Display a list of users


You can now pass the user information you want to display in the form of the prop at the List component. Use JavaScript's map function to traverse the array data.

2 => (
3 <List
4 key={}
5 userAvatar={item.picture.large}
6 firstName={}
7 lastName={}
8 />
9 ));

These props are then passed as arguments to the List component.

1xport default function List({ userAvatar, firstName, lastName }) {
2 const classes = useStyles();
4 return (
5 <Grid container spacing={2}>
6 <Grid item className={classes.root} xs={12}>
7 <Grid container justify="center" spacing={2}>
8 <Grid key={1} item>
9 <Paper className={classes.paper} elevation={2}>
10 <Grid container justify="center">
11 <Avatar
12 alt="User"
13 className={classes.avatarImage}
14 src={userAvatar}
15 />
16 <Typography variant="p">
17 Name: {firstName} {lastName}
18 </Typography>
19 </Grid>
20 </Paper>
21 </Grid>
22 </Grid>
23 </Grid>
24 </Grid>
25 );

The above code snippet uses few more core components from Material UI (such as Avatar). You can find their reference in the official documentation of this library.

Here is the final result you are going to get when you back to the browser screen.




I hope in this post, you get the idea of how you can leverage the core components of Material UI library in your React apps and save a ton of development time.


More Posts

Browse all posts

Aman Mittal author

I'm a software developer and a technical writer. In this blog, I write about Technical writing, Node.js, React Native and Expo.

Currently, working at Expo. Previously, I've worked as a Developer Advocate, and Senior Content Developer with companies like Draftbit, Vercel and Crowdbotics.

Copyright ©  2019-2022 Aman Mittal · All Rights Reserved.