Open In App

How to show upload or download percentage in ReactJS ?

Last Updated : 28 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Material-UI’s CircularProgress component provides a visual representation of progress using a circular loading indicator. We can show upload or download percentage in ReactJS by using this with some core logic to show percentage. Material UI for React has this component available for us, and it is very easy to integrate. We can do it the ReactJS using the following approach.

Prerequisites:

Steps to create 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

Example: 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 Box from '@material-ui/core/Box';
import CircularProgress from '@material-ui/core/CircularProgress';
 
export default function App() {
 
  const [uploadOrDownloadCount,
         setUploadOrDownloadCount] = React.useState(10);
 
  React.useEffect(() => {
    const timer = setInterval(() => {
      setUploadOrDownloadCount(
        (beforeValue) => (beforeValue >= 100 ? 0
                          : beforeValue + 10));
    }, 800);
    return () => {
      clearInterval(timer);
    };
  }, []);
 
  return (
    <div>
      <h4>How to show upload/download percentage in ReactJS?</h4>
      <Box position="relative" display="inline-flex">
        <CircularProgress variant="determinate"
                          value={uploadOrDownloadCount} />
        <Box
          bottom={0}
          right={0}
          top={0}
          justifyContent="center"
          left={0}
          display="flex"
          alignItems="center"
          position="absolute"
        >
          {`${Math.round(uploadOrDownloadCount)}%`}
        </Box>
      </Box>
    </div>
  );
}


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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads