Open In App

How to create progress bar in React JS ?

Improve
Improve
Like Article
Like
Save
Share
Report

A progress bar shows the measure of progress of any task or activity. It is the graphical representation of progression. Material UI for React has this component available for us and is very easy to integrate. We can Create a straightforward Progress Bar in React JS using the following approach.

Prerequisites:

Steps to Create the React Application And Installing Module:

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

Step 3: After creating the React JS application, Install the material-ui modules using the following command:

npm install @material-ui/core
npm install @material-ui/icons

Project Structure:

Project Structure

Example: Write down the following code in the App.js file.

Javascript




import React from "react";
import { useTheme }
    from "@material-ui/core/styles";
import KeyboardArrowLeft
    from "@material-ui/icons/KeyboardArrowLeft";
import MobileStepper
    from "@material-ui/core/MobileStepper";
import KeyboardArrowRight
    from "@material-ui/icons/KeyboardArrowRight";
import Button
    from "@material-ui/core/Button";
 
const App = () => {
    const theme = useTheme();
    const [progressCount, setCurrentStepCount] = React.useState(0);
 
    const handleBack = () => {
        setCurrentStepCount(
            (prevActiveStep) =>
                prevActiveStep - 1);
    };
 
    const handleNext = () => {
        setCurrentStepCount(
            (prevActiveStep) =>
                prevActiveStep + 1);
    };
 
    return (
        <div
            style={{
                marginLeft: "40%",
            }}
        >
            <h2>How to show Progress Bar in ReactJS?</h2>
            <MobileStepper
                steps={6}
                activeStep={progressCount}
                position="static"
                variant="progress"
                style={{
                    maxWidth: 400,
                    flexGrow: 1,
                }}
                backButton={
                    <Button
                        size="small"
                        onClick={handleBack}
                        disabled={progressCount === 0}>
                        {theme.direction !== "rtl" ? (
                            <KeyboardArrowLeft />
                        ) : (
                            <KeyboardArrowRight />
                        )}
                        Decrease (-)
                    </Button>
                }
                nextButton={
                    <Button
                        size="small"
                        onClick={handleNext}
                        disabled={progressCount === 5}>
                        Increase (+)
                        {theme.direction !== "rtl" ? (
                            <KeyboardArrowRight />
                        ) : (
                            <KeyboardArrowLeft />
                        )}
                    </Button>
                }
            />
        </div>
    );
};
 
export default App;


Step to Run Application: Run the application using the following command from the root directory of the project.

npm start

Output: Now open your browser and go to http://localhost:3000/



Last Updated : 24 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads