Monday, September 23, 2024
Google search engine
HomeLanguagesJavascriptHow to show upload or download percentage in ReactJS ?

How to show upload or download percentage in ReactJS ?

We can show upload or download percentage in ReactJS using the CircularProgress Component 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.

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

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 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.

RELATED ARTICLES

Most Popular

Recent Comments