How to create an Avatar generator app in ReactJS ?
Last Updated :
29 Jul, 2024
In this article, we are going to make a simple avatar generator app that generates random images. In this avatar generator, we have several buttons for different categories to choose from, the next button to generate random images, and a download button to finally download your favorite avatar.
Prerequisites: The pre-requisites for this project are:
- React
- Functional components
- React Hooks
- React Axios & API
- Javascript ES6
Basic setup: Start a project by the following command:
npx create-react-app avatarApp
Now, go to the project folder i.e avatarApp:
cd avatarApp

Now, go to the src folder and create a Components folder and a Styles folder. Under the Components folder, create a file 'Avatar.jsx' and under the Styles folder, create a file 'Avatar.css'

- Now, open the console and install the Axios npm package:
npm install axios
JavaScript
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
- App.js: App component renders a single Avatar component
JavaScript
import './App.css';
import Avatar from './Components/Avatar';
function App() {
return (
<div className="App">
<Avatar />
</div>
);
}
export default App;
- App.css: This sets the background of our app to a nice CSS gradient
.App {
margin: 0;
padding: 0;
background-color: #b8c6db;
background-image: linear-gradient(315deg, #b8c6db 0%, #f5f7fa 74%);
}
- Avatar.jsx: This file contains all the logic. We will be using a free opensource API (no auth required) called 'DiceBear Avatars' to fetch random avatars based on several parameters.
JavaScript
import React, { useState } from 'react';
import '../Styles/Avatar.css';
import Axios from 'axios';
const Avatar = () => {
// Setting up the initial states using react hook 'useState'
const [sprite, setSprite] = useState("bottts");
const [seed, setSeed] = useState(1000);
// Function to set the current sprite type
function handleSprite(spritetype) {
setSprite(spritetype);
}
// Function to generate random seeds for the API
function handleGenerate() {
let x = Math.floor(Math.random() * 1000);
setSeed(x);
}
// Function to download image and save it in our computer
function downloadImage() {
Axios({
method: "get",
url: `https://avatars.dicebear.com/api/${sprite}/${seed}.svg`,
responseType: "arraybuffer"
})
.then((response) => {
var link = document.createElement("a");
link.href = window.URL.createObjectURL(
new Blob([response.data],
{ type: "application/octet-stream" })
);
link.download = `${seed}.svg`;
document.body.appendChild(link);
link.click();
setTimeout(function () {
window.URL.revokeObjectURL(link);
}, 200);
})
.catch((error) => { });
}
return (
<div className="container">
<div className="nav">
<p>Random Avatar Generator</p>
</div>
<div className="home">
<div className="btns">
<button onClick={() => {
handleSprite("avataaars") }}>Human</button>
<button onClick={() => {
handleSprite("human") }}>Pixel</button>
<button onClick={() => {
handleSprite("bottts") }}>Bots</button>
<button onClick={() => {
handleSprite("jdenticon") }}>Vector</button>
<button onClick={() => {
handleSprite("identicon") }}>Identi</button>
<button onClick={() => {
handleSprite("gridy") }}>Alien</button>
<button onClick={() => {
handleSprite("micah") }}>Avatars</button>
</div>
<div className="avatar">
<img src=
{`https://avatars.dicebear.com/api/${sprite}/${seed}.svg`} alt="Sprite" />
</div>
<div className="generate">
<button id="gen" onClick={() => {
handleGenerate() }}>Next</button>
<button id="down" onClick={() => {
downloadImage() }}>Download</button>
</div>
</div>
</div>
)
}
export default Avatar;
- Avatar.css: Use this file to decorate our app
@import url('https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@400;600;700&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Zen+Tokyo+Zoo&display=swap');
.nav{
height: 6vh;
width: 100%;
background-color: #313442;
display: flex;
justify-content: center;
align-items: center;
color: white;
font-family: 'Zen Tokyo Zoo', cursive;
font-size: 35px;
}
.home{
box-sizing: border-box;
height: 94vh;
width: 100%;
gap: 15px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.avatar{
height: 50%;
width: 50%;
max-width: 400px;
max-height: 400px;
margin-top: 40px;
margin-bottom: 45px;
}
.btns{
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
}
button{
width: 6em;
height: 2.5em;
margin: 10px;
font-size: 20px;
font-weight: 600;
font-family: 'Roboto Mono', monospace;
background-color: rgb(231, 231, 231);
box-shadow: 2px 3px 5px rgb(102, 101, 101);
border-radius: 15px;
border: none;
transition: 0.2s;
}
button:active{
box-shadow: none;
}
.btns > button:hover{
background-color: #ffffff;
}
#gen{
background-color: #4361ee;
color: white;
}
#down{
background-color: #EB3349;
color: white;
}
- Save all the files and start the server:Â
npm start
Open http://localhost:3000/ URL in the browser. It will display the result. Our app is now complete and it should be working now.
Similar Reads
How to Fetch Data From an API in ReactJS?
ReactJS provides several ways to interact with APIs, allowing you to retrieve data from the server and display it in your application. In this article, weâll walk you through different methods to fetch data from an API in ReactJS, including using the built-in fetch method, axios, and managing the st
8 min read
Create a QR code generator app using ReactJS
In this article, we will create a simple QR Code generator app. A QR code is a two-dimensional barcode that can be read by smartphones. It allows the encoding of more than 4,000 characters in a compact format. QR codes can be used for various purposes, such as displaying text to users, opening URLs,
3 min read
How to create a form in React?
React uses forms to allow users to interact with the web page. In React, form data is usually handled by the components. When the data is handled by the components, all the data is stored in the component state. You can control changes by adding event handlers in the onChange attribute and that even
5 min read
How to create Avatar in react-native ?
In this article, we will create 3 different kinds of Avatars using the react-native-paper library. An avatar is used for representation purposes in the form of icons, images, or text. In our project, we will create all these Avatars using material design. To give you a better idea of what weâre goin
6 min read
How are forms created in ReactJS ?
Form is a document that stores information of a user on a web server using interactive controls. A form contains different kinds of information such as username, password, contact number, email ID, etc. Creating a form in React is almost similar to that of HTML if we keep it simple and just make a s
3 min read
How to use Avatar Component in ReactJS?
Avatars are found throughout material design with uses in everything from tables to dialog menus. Material UI for React has this component available for us, and it is very easy to integrate. We can use the Avatar Component in ReactJS using the following approach. Prerequisites:NodeJS or NPMReactJSSt
2 min read
Create Jokes Generator App using React-Native
In this article, we are going to build a jokes generator app using react native. React Native enables you to master the designing an elegant and dynamic useÂr interface while eÂffortlessly retrieving jokeÂs from external APIs. Let's take a look at how our completed project will lookPrerequisites /
3 min read
Create a meme generator by using ReactJS
In this tutorial, weâll create a meme generator using ReactJS. In the meme generator, we have two text fields in which we enter the first text and last text. After writing the text when we click the Gen button, it creates a meme with an image and the text written on it. Preview Image: meme generator
3 min read
Create Memes Generator App using React-Native
The MeÂme Generator App is a mobile application that allows users to effortlessly create memes. With its useÂr-friendly interface, useÂrs can choose from a wide collection of popular meÂme templates and add their own customized text to the top and bottom. In this article, we will see how we can bui
4 min read
How to create components in ReactJS ?
Components in React JS is are the core of building React applications. Components are the building blocks that contains UI elements and logic, making the development process easier and reusable. In this article we will see how we can create components in React JS. Table of Content React Functional C
3 min read