Open In App

React MUI Avatar 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 are going to discuss the React MUI Avatar API. Avatars are profile pictures that are usually used for logged-in users. Avatars are found usually from tables to dialog menus. The API provides a lot of functionality and we will learn to implement them.



Import Avatar API

import Avatar from '@mui/material/Avatar';
// or
import { Avatar } 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.



Syntax: Create an Avatar as follows:

<Avatar alt="GeeksforGeeks" 
    src="/static/images/avatar/gfgIcon.jpg" />

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 @emotion/styled @mui/lab

Step 4: Run the project as follows:

npm start

Example 1: In the following example, we have a basic Avatar.




import "./App.css";
import React from "react";
import Avatar from "@mui/material/Avatar";
function App() {
    return (
        <div className="App">
            <div
                className="head"
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <h1 style={{
                    color: "green",
                }}>
                    GeeksforGeeks
                </h1>
            </div>
            <div style={{
                width: "fit-content",
                margin: "auto",
            }}>
                <strong>React MUI Avatar API</strong>
            </div>
  
            <center>
                <Avatar
                    alt="GeeksforGeeks"
                    src=
                />
            </center>
        </div>
    );
}
  
export default App;

Output:

 

Example 2: In the following example, we have different shape avatars.




import "./App.css";
import React from "react";
import Avatar from "@mui/material/Avatar";
function App() {
    return (
        <div className="App">
            <div className="head"
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}>
                <h1 style={{
                    color: "green",
                }}>
                    GeeksforGeeks
                </h1>
            </div>
            <div style={{
                width: "fit-content",
                margin: "auto",
            }}>
                <strong>React MUI Avatar API</strong>
            </div>
            <br />
            <div style={{
                margin: "auto",
                display: "flex",
                justifyContent: "space-evenly",
            }}>
                <Avatar
                    variant="circular"
                    alt="GeeksforGeeks"
                    src=
                />
                <Avatar
                    variant="rounded"
                    alt="GeeksforGeeks"
                    src=
                />
                <Avatar
                    variant="square"
                    alt="GeeksforGeeks"
                    src=
                />
            </div>
        </div>
    );
}
  
export default App;

Output:

 

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


Article Tags :