How to Create an Unique ID in ReactJS ?
Last Updated :
09 Jan, 2025
Generating an unique ID in React js is helpful in component identifiers in React applications. It can be used to separate and identify database records and as a key in many applications.
How to Create Unique ID in React
We can create unique id in React With the help of UUID package, that generates unique ids, and we can also use the timestamps for the unique id as it is not repeating and keeps on changing. Generating unique IDs is essential for keying list items in React.
These method are given below in detail.
Steps to Create React App
Step 1: Create a React application using the following command:
npx create-react-app foldername
Step 2: After creating your project folder i.e. foldername, move to it using the following command:
cd foldername
Project Structure:
After creating, your project directory should look like this.
Project Directory
Approach 1: Using UUId
We will be using Uuid V4 in this tutorial. Uuid v4 is a React library or package that creates a universally unique identifier (UUID). It is a 128-bit unique identifier generator. The bits that comprise a UUID v4 are generated randomly and with no inherent logic. Because of this, there is no way to identify information about the source by looking at the UUID, so it is very unique.
Install UUID v4
use the following command in the terminal in project directory
npm install uuidv4
Example: Using uuidv4 to get a new unique if on every reload or re-render. also can be sliced into smallar string if required.
CSS
/* App.css */
.App {
text-align: center;
overflow-y: scroll;
}
.geeks {
color: green;
}
.container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
}
.item {
min-width: 10rem;
text-align: left;
}
JavaScript
// App.js
//import uuid v4
import { v4 as uuid } from "uuid";
import "./App.css";
export default function App() {
// New unique id
const unique_id = uuid();
// Get first 8 characters using slice
const small_id = unique_id.slice(0, 8);
return (
<div className="App">
<h1 className="geeks">GeeksforGeeks</h1>
<div className="container">
<div className="item">
<h2>Unique ID</h2>
<p>{unique_id}</p>
<h3>Sliced Unique ID</h3>
<p>{small_id}</p>
</div>
</div>
</div>
);
}
Step to run the application: To run the app, use the following command
npm start
Output: Now if you run the app, the unique id will be shown below the heading. Each time you refresh, a new and unique id will be shown with no repetition.

Approach 2: Using timestamps
Using Time stamps is a simple method to create a unique id.It is globally applicable and kept on increasing continuously. By converting it according to the needs it can be a good option for using as a unique id.
Example: Time stamp of current time is converted to alphanumeric and used as a unique id.
CSS
/* App.css */
.App {
text-align: center;
overflow-y: scroll;
}
.geeks {
color: green;
}
.container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
}
.item {
min-width: 10rem;
text-align: left;
}
JavaScript
// App.js
import React from "react";
import "./App.css";
function getUID() {
// Get the timestamp and convert
// it into alphanumeric input
return Date.now().toString(36);
}
export default function App() {
// New unique id
let day = getUID();
return (
<div className="App">
<h1 className="geeks">GeeksforGeeks</h1>
<div className="container">
<div className="item">
<h2>Unique ID</h2>
<p>{day}</p>
</div>
</div>
</div>
);
}
Step to run the application: To run the app, use the following command
npm start
Output: Now if you run the app, the unique id will be shown below the heading. Each time you refresh, a new and unique id will be shown with no repetition.

Similar Reads
How To Create a Website in ReactJS?
ReactJS is one of the most popular JavaScript libraries for building user interfaces. It allows you to create dynamic, reusable UI components and efficiently manage state and events. In this article, we'll walk through the steps to create a basic website using ReactJS.PrerequisitesNPM & Node.jsR
5 min read
How to create an event in React ?
In the development of contemporary webpages, user interactions play a pivotal role. These interactions include mouse clicks, keypresses, or even uncommon events like connecting a battery to a charger. As developers, our task is to actively "listen" to these events and subsequently ensure that our ap
2 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 Tabs in ReactJS ?
Tabs make it easy to explore and switch between different views. Material UI for React has this component available for us and it is very easy to integrate. We can use Tabs Component in ReactJS using the following approach.Prerequisites:NPM & Node.jsReact JSMaterial UIwe have these approaches to
4 min read
How to get an Element by ID in ReactJS ?
ReactJS, a popular JavaScript library for user interfaces, empowers developers with tools, such as the ref system, to manipulate the DOM by accessing elements using their unique IDs. This article explores the process of obtaining elements by ID in ReactJS for effective component interaction and mani
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
How to use events in ReactJS ?
Modern webpages rely on user interactions, triggering events like clicks or keypresses. React facilitates event handling with built-in methods, allowing developers to create listeners for dynamic interfaces and responses. JavaScript events, inherent to the DOM, use bubbling propagation by default, m
2 min read
How to use Firestore Database in ReactJS ?
Firebase is a comprehensive backend solution offered by Google that simplifies the process of building, managing, and growing applications. When developing mobile or web apps, handling the database, hosting, and authentication can be challenging tasks. Firebase addresses these challenges by providin
9 min read
How to use Badge Component in ReactJS?
Badge generates a small badge to the top-right of its children component. Material UI for React has this component available for us, and it is very easy to integrate. We can use the Badge Component in ReactJS using the following approach. Prerequisites:NodeJS or NPMReact JSMaterial UISteps to Create
2 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