Open In App

React MUI Icon API

MUI or Material-UI is a UI library providing predefined robust and customizable components for React for easier web development. The MUI design is based on top of Material Design by Google.

In this article, we will discuss the React MUI Icon API. The icon element is used to create different icons of different shapes and sizes with various colors. The icons to display are provided in the MUI library. The API provides a lot of functionality and we will learn to implement them.



Import Icon API:

import Icon from '@mui/material/Icon';
// or
import { Icon } from '@mui/material';

Props List: Here is the list of different props used with this component. We can access them and modify them according to our needs.



CSS Rules:

Syntax: Create an Icon element as follows:

Importing API:

import HomeIcon from '@mui/icons-material/Home';
<HomeIcon />

Installing and Creating React app, and adding the MUI dependencies:

Step 1: Create a react project using the following command.

npx create-react-app gfg_tutorial

Step 2: Get into the project directory

cd gfg_tutorial

Step 3: Install the MUI dependencies as follows:

npm install @mui/material @emotion/react 
npm install @emotion/styled @mui/lab @mui/icons-material

Project Structure: The project should look like the below:

 

Step 4: Run the project as follows:

npm start

Example 1: In the following example, we have Icon elements.




import "./App.css";
import * as React from "react";
import { Code, Computer, Home, Web }
    from "@mui/icons-material";
  
function App() {
    return (
        <div className="App">
            <div
                className="head"
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <h1
                    style={{
                        color: "green",
                    }}
                >
                    GeeksforGeeks
                </h1>
                <strong>
                  React MUI Icon API
                </strong>
            </div>
            <br />
            <div
                style={{
                    margin: "auto",
                    display: "flex",
                    justifyContent: "space-evenly",
                }}
            >
                <Home />
                <Web />
                <Code />
                <Computer />
            </div>
        </div>
    );
}
export default App;

Output:

 

Example 2: In the following example, we have Icons of different sizes and colors.




import "./App.css";
import * as React from "react";
import { Code, Computer, Home, Web } from
    "@mui/icons-material";
  
function App() {
    return (
        <div className="App">
            <div
                className="head"
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <h1
                    style={{
                        color: "green",
                    }}
                >
                    GeeksforGeeks
                </h1>
                <strong>React MUI Icon API</strong>
            </div>
            <br />
            <div
                style={{
                    margin: "auto",
                    display: "flex",
                    justifyContent: "space-evenly",
                }}
            >
                <Home fontSize="large" 
                   color="error" />
                <Web fontSize="medium"
                   color="warning" />
                <Code fontSize="small" 
                   color="primary" />
            </div>
        </div>
    );
}
export default App;

Output:

 

Reference: https://mui.com/material-ui/api/icon/


Article Tags :