Skip to content
Related Articles
Open in App
Not now

Related Articles

How to use CircularProgress Component in ReactJS?

Improve Article
Save Article
  • Last Updated : 25 Jan, 2023
Improve Article
Save Article

As we know that 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.

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

Project Structure: It will look like the following.

Project Structure

Example 1: In this example, we will create an indeterminate CircularProgress component that shows up when an API is in progress. Please update App.js like below.

Filename: App.js

Javascript




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(
            .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: In this example, we will create different CircularProgress components of determinate nature having different values. Please update App.js file like below:

Filename: App.js

Javascript




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:

npm start

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

 

Reference: https://mui.com/material-ui/react-rating/#main-content


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!