Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to use CircularProgress Component in ReactJS?

  • Last Updated : 22 Jan, 2021

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:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

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

Project Structure: It will look like the following.

Project Structure

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, {useEffect, useState} from 'react';
import CircularProgress from '@material-ui/core/CircularProgress';
  
const App = () => {
  
  useEffect(()=> {
    getDataFromAPI()
  }, [])
  
  const [isLoading, setIsLoading] = useState(true)
  
  // Sample API to fetch Data
  const getDataFromAPI = () => {
    console.log("API called!!")
     .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:

This is how we can show the loading effect in ReactJS using the CircularProgress component.




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!