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
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 Fade Component in React JS ?
The Fade Component, available in Material UI for React, seamlessly adds a fade animation to a child element or component. Integrating this component into a React JS project is straightforward and enhances the user interface with ease.Prerequisites:React JSMaterial UIPopper Transitions: The Transitio
2 min read
How to use Skeleton Component in React JS ?
When data is not yet loaded, a Skeleton Component serves as a placeholder preview for the user. Material UI for React provides an easily integratable version of this component, and in React JS, the following approach can be employed to utilize the Skeleton Component.Prerequisites:React JSMaterial UI
2 min read
How to create a Speed Dial Component in ReactJS ?
The Material-UI Lab hosts new and exciting components that arenât fully ready for the core library. A Speed Dial component is like a dialog with multiple floating action buttons. It can be used to make any primary actions like share, copy, print, etc. more accessible and make the user experience bet
3 min read
How to use Grow Component in ReactJS?
Grow Component in ReactJS adds a Grow animation to a child element or component. Material UI for React has this component available for us, and it is very easy to integrate. We can use the Grow Component in ReactJS using the following approach.PrerequisitesReact JSMaterial UIApproachTo implement the
2 min read
ReactJS ReactStrap Tooltip Component
Reactstrap is a popular front-end library that is easy to use React Bootstrap 4 components. This library contains the stateless React components for Bootstrap 4. The Tooltip component helps in displaying informative text when users hover over, focus on, or tap an element. We can use the following ap
4 min read
How to create Class Component in React?
JavaScript syntax extension permits HTML-like code within JavaScript files, commonly used in React for defining component structure. It simplifies DOM element manipulation by closely resembling HTML syntax. JSX facilitates the creation of reusable UI building blocks, defined as JavaScript functions
2 min read
How to use Slide Component in ReactJS ?
Slide Component adds a Slide animation to a child element or component. Material UI for React has this component available for us, and it is very easy to integrate. We can use the Slide Component in ReactJS using the following approach.Prerequisites:NodeJS or NPMReactJSMaterial UISteps to Create the
3 min read
ReactJS Reactstrap Spinners Component
Reactstrap is a popular front-end library that is easy to use React Bootstrap 4 components. This library contains the stateless React components for Bootstrap 4. The Spinner component allows the user to show the loading effect. It is used for the purpose of indicating a loading state. We can use the
3 min read
How to use CssBaseLine Component in ReactJS ?
In React JS, A CssBaseline component is a collection of HTML elements and attribute style-normalizations. At the <head> of our document, it is added as CSS reset. This component is used to add some basic styling to our application like box-sizing, background color, etc like CSS reset for our a
3 min read