How to make mui avatar with random color in react js?
April 24, 2023Hi Friends 👋,
Welcome To aGuideHub!
To make mui avatar with random color in React js, you can use this code sx: {bgcolor: stringToColor(name)}
. It will make mui avatar with random color in react js.
Today, I am going to show you, How to make mui avatar with random color in react js.
Installation
Install the following packages to use mui avatar in react js.
npm
npm install @mui/material @emotion/react @emotion/styled
yarn
yarn add @mui/material @emotion/react @emotion/styled
Table of contents
- Install MUI and create a new React app.
- Import the Avatar component from MUI.
- Use the Avatar component in your JS code.
- Add inline styles to adjust the random color.
Step 1: Install MUI and create a new React app.
First you have to install the React project. You should use create-react-app
command to create a new React project.
npx create-react-app my-app
cd my-app
npm start
Step 2: Import the Avatar component from MUI.
After installing MUI
, you have to import your React component. To do this, add the following line to the top of your component file.
import * as React from "react";
import Avatar from "@mui/material/Avatar";
import Stack from "@mui/material/Stack";
Step 3: Use the Avatar component in your JS code.
You can use the Avatar
component in your react js. For example, you can have the following code to display an avatar with an <Avatar {...stringAvatar("Guide Hub")} />
.
<Stack direction="row" spacing={2}>
<Avatar {...stringAvatar("Guide Hub")} />
<Avatar {...stringAvatar("Viraj Mishra")} />
<Avatar {...stringAvatar("Astha Mishra")} />
</Stack>
Step 4: Add inline styles to adjust the random color.
The stringAvatar
function takes a name string
and returns an object that has an sx
property that contains bgcolor
.
function stringAvatar(name) {
return {
sx: {
bgcolor: stringToColor(name)
},
children: `${name.split(" ")[0][0]}${name.split(" ")[1][0]}`
};
}
MUI material make mui avatar random color example.
The below code is an example of making random color of mui avatar using, stringAvatar
function takes a name string and returns an object
that contains an sx
property with a bgcolor
attribute set to the color generated by the stringToColor
function and a children
property with the first and last initials of the name.
App.js
import * as React from "react";
import Avatar from "@mui/material/Avatar";
import Stack from "@mui/material/Stack";
function stringToColor(string) {
let hash = 0;
let i;
/* eslint-disable no-bitwise */
for (i = 0; i < string.length; i += 1) {
hash = string.charCodeAt(i) + ((hash << 5) - hash);
}
let color = "#";
for (i = 0; i < 3; i += 1) {
const value = (hash >> (i * 8)) & 0xff;
color += `00${value.toString(16)}`.slice(-2);
}
/* eslint-enable no-bitwise */
return color;
}
function stringAvatar(name) {
return {
sx: {
bgcolor: stringToColor(name)
},
children: `${name.split(" ")[0][0]}${name.split(" ")[1][0]}`
};
}
export default function BackgroundLetterAvatars() {
return (
<Stack direction="row" spacing={2}>
<Avatar {...stringAvatar("Guide Hub")} />
<Avatar {...stringAvatar("Viraj Mishra")} />
<Avatar {...stringAvatar("Astha Mishra")} />
</Stack>
);
}
In the above code example, I have used the @mui/material
component and made mui avatar with random color in react js.
Check the output of the above code example.
Here, we are provided code sandbox links for the above program make mui avatar with random color in react js. Then you can use whenever you want and do the changes as per your requirements.
All the best 👍