Skip to content
Related Articles

Related Articles

Improve Article

How to create progress bar in ReactJS ?

  • Last Updated : 12 Feb, 2021

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 it is very easy to integrate. We can Create Progress Bar in ReactJS using the following approach.

Creating 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 ReactJS application, Install the material-ui modules using the following command:



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

App.js: Now write down the following code in the App.js file. Here, App is our default component where we have written our code.

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/, you will see the following output.




My Personal Notes arrow_drop_up
Recommended Articles
Page :