DEV Community

Cover image for Getting Started with Material UI in React: An Introduction
Tien Nguyen
Tien Nguyen

Posted on

Getting Started with Material UI in React: An Introduction

Material UI is a popular UI library for React that provides pre-designed components following Google's Material Design principles. It offers a wide range of tools to make it easy for developers to create beautiful, modern user interfaces. In this post, I will introduce you to Material UI and guide you through the steps to get started with it in your React project.

Installation

Before you can start using Material UI in your React project, you need to install it. You can do this by running the following command in your terminal:

npm install @mui/material @emotion/react @emotion/styled
// or
yarn add @mui/material @emotion/react @emotion/styled
Enter fullscreen mode Exit fullscreen mode

This command will install the core components of Material UI in your project.

Usage

Once you have installed Material UI, you can start using its components in your React project. You need to import the component you want to use from the Material UI library and then use it in your JSX code.

Here is an example of how to use the Button component from Material UI:

import React from 'react';
import Button from '@mui/material/Button';

function App() {
  return (
    <Button variant="contained" color="primary">
      Click me
    </Button>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

In this example, we import the Button component from the Material UI library and use it in the JSX code of our functional component. We set the variant and color props to customize the button's appearance.

Theming

Material UI also provides theming options to allow you to customize the look and feel of your app easily. You can define your custom theme using the createTheme function, which returns a theme object that you can customize.

Here is an example of how to customize the primary and secondary colors of your Material UI theme:

import { createTheme } from '@mui/material/styles';

const theme = createTheme({
  palette: {
    primary: {
      main: '#ff4081',
    },
    secondary: {
      main: '#3f51b5',
    },
  },
});

Enter fullscreen mode Exit fullscreen mode

After defining the theme, you can pass it to your app using the ThemeProvider component, like this:

import React from 'react';
import { ThemeProvider, createTheme } from '@mui/material/styles';
import Button from '@mui/material/Button';

const theme = createTheme({
  palette: {
    primary: {
      main: '#ff4081',
    },
    secondary: {
      main: '#3f51b5',
    },
  },
});

function App() {
  return (
    <ThemeProvider theme={theme}>
      <Button variant="contained" color="primary">
        Click me
      </Button>
    </ThemeProvider>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

In this example, we wrap our app in the ThemeProvider component and pass the theme object to it as a prop. This will apply the custom theme to all Material UI components in our app.

Conclusion

In conclusion, Material UI is an excellent choice for React developers who want to build modern and visually appealing user interfaces. Its vast selection of pre-designed components, easy-to-use theming options, and simplicity of use make it a powerful tool for creating amazing web applications.

If you want to explore another popular UI library for React, you might be interested in reading our blog post comparing Material UI vs Ant Design. In this post, I compare and contrast the features of both libraries, so you can choose the best one for your project. Happy coding!

Top comments (0)