How to create Stepper Component Using React JS ?
Last Updated :
24 Apr, 2025
The checkout process on e-commerce websites often involves a series of steps, commonly represented by a stepper component. In this tutorial, we will guide you through creating a customizable stepper component using React, allowing you to easily integrate it into your web applications.
Preview of final output: Let us have a look at how the final output will look like.
Project Preview
Prerequisites
Approach
Our approach will involve a configuration-driven UI, where we define the steps and their corresponding components in a configuration object. This allows for flexibility and easy customization. We will count each step and accordingly the completion graph will change and all passing through all the steps it will shows the completion of the stepper.
Steps to Create the React Application
Step 1: Set Up Your React App with Vite
npm create vite@latest

Step 2: Navigate to the Project Directory
cd stepper
Step 3: Install the project dependencies using:
npm install
Project Structure:
project structureDependencies:
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/react": "^18.2.55",
"@types/react-dom": "^18.2.19",
"@vitejs/plugin-react-swc": "^3.5.0",
"eslint": "^8.56.0",
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.5",
"vite": "^5.1.0"
}
Example: Create the required files and add the following code.
JavaScript
// CheckoutStepper.jsx
import { useEffect, useRef, useState } from "react";
const CheckoutStepper = ({ stepsConfig = [] }) => {
const [currentStep, setCurrentStep] = useState(1);
const [isComplete, setIsComplete] = useState(false);
const [margins, setMargins] = useState({
marginLeft: 0,
marginRight: 0,
});
const stepRef = useRef([]);
useEffect(() => {
setMargins({
marginLeft: stepRef.current[0].offsetWidth / 2,
marginRight: stepRef.current[stepsConfig.length - 1].offsetWidth / 2,
});
}, [stepRef, stepsConfig.length]);
if (!stepsConfig.length) {
return <></>;
}
const handleNext = () => {
setCurrentStep((prevStep) => {
if (prevStep === stepsConfig.length) {
setIsComplete(true);
return prevStep;
} else {
return prevStep + 1;
}
});
};
const calculateProgressBarWidth = () => {
return ((currentStep - 1) / (stepsConfig.length - 1)) * 100;
};
const ActiveComponent = stepsConfig[currentStep - 1]?.Component;
return (
<>
<div className="stepper">
{stepsConfig.map((step, index) => {
return (
<div
key={step.name}
ref={(el) => (stepRef.current[index] = el)}
className={`step ${currentStep >
index + 1 || isComplete ?
"complete" : ""
} ${currentStep === index + 1 ? "active" : ""} `}
>
<div className="step-number">
{currentStep > index + 1 || isComplete ? (
<span>✓</span>
) : (
index + 1
)}
</div>
<div className="step-name">{step.name}</div>
</div>
);
})}
<div
className="progress-bar"
style={{
width: `calc(100% - ${margins.marginLeft
+ margins.marginRight}px)`,
marginLeft: margins.marginLeft,
marginRight: margins.marginRight,
}}
>
<div
className="progress"
style={{ width: `${calculateProgressBarWidth()}%` }}
></div>
</div>
</div>
<ActiveComponent />
{!isComplete && (
<button className="btn" onClick={handleNext}>
{currentStep === stepsConfig.length ? "Finish" : "Next"}
</button>
)}
</>
);
};
export default CheckoutStepper;
JavaScript
// App.jsx
import "./App.css";
import CheckoutStepper from "./components/CheckoutStepper";
const CHECKOUT_STEPS = [
{
name: "Customer Info",
Component: () => <div>Provide your contact details.</div>,
},
{
name: "Shipping Info",
Component: () => <div>Enter your shipping address.</div>,
},
{
name: "Payment",
Component: () => <div>Complete payment for your order.</div>,
},
{
name: "Delivered",
Component: () => <div> Your order has been delivered.</div>,
},
];
function App() {
return (
<div>
<h2>Checkout</h2>
<CheckoutStepper stepsConfig={CHECKOUT_STEPS} />
</div>
);
}
export default App;
JavaScript
// main.jsx
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App.jsx";
ReactDOM.createRoot(document.getElementById("root")).render(
<React.StrictMode>
<App />
</React.StrictMode>
);
CSS
/* App.css */
body {
font-family: sans-serif;
}
.stepper {
position: relative;
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
}
.step {
display: flex;
flex-direction: column;
align-items: center;
}
.step-number {
width: 30px;
height: 30px;
border-radius: 50%;
background-color: #ccc;
display: flex;
justify-content: center;
align-items: center;
margin-bottom: 5px;
z-index: 2;
}
.step-name {
font-size: 14px;
}
.active .step-number {
background-color: #007bff;
color: #fff;
}
.complete .step-number {
background-color: #28a745;
color: #fff;
}
.progress-bar {
position: absolute;
top: 25%;
left: 0;
height: 4px;
background-color: #ccc;
}
.progress {
height: 100%;
background-color: #28a745;
transition: 0.2s ease;
}
Steps to run the application:
Step 1: Type the following command in the terminal.
npm run dev
Step 2: Open web browser and type the following URL.
http://localhost:5173/
Output:
output
Similar Reads
JavaScript Tutorial
JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. JavaScript is an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side : On client sid
11 min read
Web Development
Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers
React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
HTML Tutorial
HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript
10 min read
JavaScript Interview Questions and Answers
JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial
React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
HTML Interview Questions and Answers
HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read
NodeJS Interview Questions and Answers
NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net
15+ min read
What is an API (Application Programming Interface)
In the tech world, APIs (Application Programming Interfaces) are crucial. If you're interested in becoming a web developer or want to understand how websites work, you'll need to familiarize yourself with APIs. Let's break down the concept of an API in simple terms.What is an API?An API is a set of
10 min read
Introduction of Firewall in Computer Network
A firewall is a network security device either hardware or software-based which monitors all incoming and outgoing traffic and based on a defined set of security rules it accepts, rejects, or drops that specific traffic. It acts like a security guard that helps keep your digital world safe from unwa
10 min read