Build a Random User Generator App Using ReactJS
Last Updated :
24 Jul, 2024
In this article, we will create a random user generator application using API and React JS. A Random User Generator App Using React Js is a web application built with the React.js library that generates random user profiles. It typically retrieves and displays details like names, photos, and contact information to simulate user data for testing and design purposes.
Preview Image:
PreviewSteps to Create a React Application:
Step 1: Create a react application by using this command
npx create-react-app RandomUserApp
Step 2: After creating your project folder, i.e. RandomUserApp, use the following command to navigate to it:
cd RandomUserApp
Project Structure:

The package.json file will look like:
{
"name": "RandomUserApp",
"version": "0.0.0",
"private": true,
"dependencies": {
"react": "18.2.0",
"react-dom": "18.2.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"devDependencies": {
"react-scripts": "latest"
}
}
Approach:
- The ReÂact component called "App" is responsible for creating an engaging Random User GeÂnerator application. It employs a well-deÂfined set of instructions to exeÂcute
- The state variables are initialized by utilizing the useState function. This allows for the storage of user data (user) and toggling the display of additional information (showMoreÂInfo).
- The useÂEffect hook is employed to feÂtch user data from a random user API when the component mounts. This ensures that the data is retrieved wheÂn the app loads.
- The useÂr data is retrieved through the getUser function from a designateÂd API.
- The function changeÂThemeColor is responsible for generating a random color and applying it to the app's theÂme through CSS custom properties.
- The function toggleÂMoreInfo is designed to alteÂrnate the state of showMoreÂInfo, enabling or disabling the visibility of additional user information.
- The JSX reÂndering of the component showcaseÂs various user details, such as their avatar, nameÂ, job title, email address, phone number, and location. In addition to this primary information, a convenient button eÂnables users to toggle visibility for more comprehensive deÂtails.
- Event handleÂrs, such as onClick for the "Get Random User" button and the "Show More Info" button, have beeÂn defined to activate speÂcific actions.
Example : Write the below code in App.js file and style.css in the src directory
CSS
/* Write CSS Here */
/* style.css */
* {
padding: 0;
margin: 0;
box-sizing: border-box;
font-family: "Poppins", sans-serif;
}
:root {
--theme-color: #5074f3;
}
body {
background-color: var(--theme-color);
}
.container {
width: 90%;
max-width: 25em;
position: absolute;
transform: translate(-50%, -50%);
top: 50%;
left: 50%;
}
.card {
width: 100%;
padding: 4em 0;
background-color: #ffffff;
text-align: center;
border-radius: 0.5em;
}
.card .img-container {
height: 11.25em;
width: 11.25em;
display: block;
margin: -8.75em auto 0 auto;
background-color: #ffffff;
box-shadow: 0 0 0 0.3em #ffffff, 0 0 0 0.9em var(--theme-color);
border-radius: 50%;
}
.img-container img {
width: 100%;
border-radius: 50%;
}
.container button {
display: block;
font-size: 1.2em;
width: 90%;
margin: 2em auto 0 auto;
padding: 1.1em 0;
border-radius: 0.3em;
border: none;
outline: none;
font-weight: 600;
color: #000341;
cursor: pointer;
}
.card h2 {
margin-top: 1.8em;
font-weight: 600;
color: #000341;
}
.card h3,
.card h4 {
font-size: 1em;
letter-spacing: 0.02em;
margin-top: 0.5em;
font-weight: 300;
color: #90919e;
}
.card i {
color: var(--theme-color);
margin-right: 0.3em;
}
.card h4 {
margin-top: 0.4em;
}
JavaScript
// App.js
import React, { useState, useEffect } from 'react';
import './style.css'; // Import the CSS file for your styles
function App() {
// Define state variables using useState
const [user, setUser] = useState({
avatar: '',
first_name: '',
last_name: '',
employment: { title: '' },
address: { city: '' },
email: '',
dob: '',
gender: '',
});
// State to toggle displaying more user info
const [showMoreInfo, setShowMoreInfo] = useState(false);
// Use useEffect to fetch user data on component mount
useEffect(() => {
getUser();
}, []);
// Function to fetch user data from the API
const getUser = () => {
const url =
'https://random-data-api.com/api/v2/users?response_type=json';
// Fetch data and update user state when data is received
fetch(url)
.then((resp) => resp.json())
.then((data) => {
setUser(data);
changeThemeColor();
});
};
// Function to change the theme color randomly
const changeThemeColor = () => {
const randomColor =
'#' + ((Math.random() * 0xffffff) << 0)
.toString(16).padStart(6, '0');
document.documentElement.style.setProperty(
'--theme-color', randomColor);
};
// Function to toggle displaying more user info
const toggleMoreInfo = () => {
setShowMoreInfo(!showMoreInfo);
};
return (
<div className="container">
<div className="card">
<div className="img-container">
<img src={user.avatar}
alt={`${user.first_name}
${user.last_name}`} />
</div>
<div className="details">
<h2>{`${user.first_name} ${user.last_name}`}</h2>
<h3>{user.employment.title}</h3>
<p>
<strong
>Email:
</strong>
{user.email}
</p>
<p>
<strong>
Phone:
</strong>
{user.phone_number}
</p>
<h4>Location:
{user.address.city}</h4>
{showMoreInfo && (
<div>
<p>
<strong>
Date of Birth:
</strong>
{user.date_of_birth}
</p>
<p>
<strong>
Gender:
</strong>
{user.gender}
</p>
</div>
)}
<button onClick={toggleMoreInfo}>
{showMoreInfo ?
'Show Less Info' : 'Show More Info'}
</button>
</div>
</div>
<button onClick={getUser}>
Get Random User
</button>
</div>
);
}
export default App;
Steps to run the Application:
- Type the following command in the terminal:
npm start
- Type the following URL in the browser:
http://localhost:3000/
Output:
Similar Reads
Build a Random Name Generator using ReactJS
In this article, a Random Name GeÂnerator will be created using React.js. Building a Random Name Generator means creating a program or application that generates random names, typically for various purposes like usernames, fictional characters, or data testing. It usually involves combining or selec
4 min read
Random Quote Generator App using ReactJS
In this article, we will create an application that uses an API to generate random quotes. The user will be given a button which on click will fetch a random quote from the API and display it on the screen. Users can generate many advices by clicking the button again. The button and the quotes are d
3 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
Build a Captcha Generator Using ReactJs
A CAPTCHA generator is a tool that creates random and visually distorted text, requiring user input to prove they are human. It prevents automated bots from accessing websites or services by testing human comprehension. Our Captcha generator geÂnerates random text-baseÂd captchas that users must acc
4 min read
Quote Generator App using NextJS
In this article, we will build an quote generator using NextJS. The user will be given a button that, when clicked, will retrieve a random quote from the API and display it on the screen. By clicking the button again, users can generate a large number of advices. Technologies Used/PrerequisitesIntro
3 min read
Create a Random User Generator using jQuery
With the use of the API and jQuery, we'll create a random user generator app. A straightforward web application called "jQuery Random User Generator" makes use of jQuery and the RandomUser.me API to generate random user data and present it in a visually appealing way. Users of this project can click
3 min read
Bill/Invoice Generator using React
Bill/Invoice Generator website using React helps users easily make, customize, and print invoices and can add or delete items. It uses React's building blocks to make the design easy to update and reuse. The user also has a feature of downloading the pdf version of the generated bill Preview of fina
5 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: PrerequisiteTh
3 min read
Color Palette Generator app using React
Color Palette Generator App using ReactJS is a web application which enables useÂrs to effortlessly geneÂrate random color palettes, vieÂw the colors, and copy the color codes to the clipboard with just a single click. There is also a search bar which allows the user to check different color themes
5 min read
How to Create a Basic Notes App using ReactJS ?
Creating a basic notes app using React JS is a better way to learn how to manage state, handle user input, and render components dynamically. In this article, we are going to learn how to create a basic notes app using React JS. A notes app is a digital application that allows users to create, manag
4 min read