Open In App
Related Articles

How to use CircularProgress Component in ReactJS?

Improve Article
Improve
Save Article
Save
Like Article
Like

Progress indicators inform users about the status of ongoing processes such as loading an app, uploading data, etc. We can use CircularProgress Component in ReactJS to show this circular loading effect. Material UI for React has this component available for us and it is very easy to integrate.

Prerequisites:

Approach:

To use CircularProgress Component in ReactJS we will install the Material UI module and import the CircularProgress component. Use this Circular progress with predefined parameters like progress and color to implement the required circular loading component.

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 i @mui/material

Project Structure:

Project Structure

The updated dependencies in package.json file will look like.

"dependencies": {
"@mui/material": "^5.14.17",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}

Example 1: This example uses circularProgress component from Material UI to implement circular loading showing data fetching from API.

Javascript




// Filename - App.js
 
import { CircularProgress } from "@mui/material";
import React, { useEffect, useState } from "react";
 
const App = () => {
    useEffect(() => {
        getDataFromAPI();
    }, []);
 
    const [isLoading, setIsLoading] = useState(true);
 
    // Sample API to fetch Data
    const getDataFromAPI = () => {
        console.log("API called!!");
        fetch("http://...com/api/v1/employees")
            .then((response) => {
                return response.json();
            })
            .then((res) => {
                setTimeout(() => {
                    setIsLoading(false);
                }, 2000);
            });
    };
 
    return (
        <div
            style={{
                marginLeft: "40%",
            }}
        >
            <h2>
                How to use CircularProgress Component in
                ReactJS?
            </h2>
            {isLoading && (
                <CircularProgress color="secondary" />
            )}
 
            {!isLoading && (
                <h3>Successfully API Loaded Data</h3>
            )}
        </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:

Example 2: This example implements multiple circular loadings for different stages using the corcularProgress components form MUI.

Javascript




// Filename - App.js
 
import { CircularProgress } from "@mui/material";
import React from "react";
 
const App = () => {
    return (
        <div>
            <h2>
                How to use CircularProgress Component in
                ReactJS?
            </h2>
 
            <div style={{ display: "flex", gap: "5px" }}>
                <CircularProgress
                    variant="determinate"
                    value={25}
                />
                <CircularProgress
                    variant="determinate"
                    value={50}
                />
                <CircularProgress
                    variant="determinate"
                    value={75}
                />
                <CircularProgress
                    variant="determinate"
                    value={100}
                />
            </div>
        </div>
    );
};
 
export default App;


Steps to run the application: Use this command in the terminal inside the project directory.

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output:


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 09 Nov, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials