Open In App

React MUI LinearProgress API

Material-UI is a UI library providing predefined robust and customizable components for React for easier web development. The MUI design is based on top of Material Design by Google.

n this article, we will discuss the React MUI LinearProgress API. Progress indicators are used to display the amount of work done with respect to the total task or just show the loading indicator. The API provides a lot of functionality and we will learn to implement them.



Props List: Here is the list of props used with this component. We can access them and modify them according to our needs.

CSS Rules:



Syntax: Create LinearProgress as follows:

<LinearProgress color="secondary"/>

Installing and Creating React app, and adding the MUI dependencies.

Step 1: Create a react project using the following command.

npx create-react-app gfg_tutorial

Step 2: Get into the project directory

cd gfg_tutorial

Step 3: Install the MUI dependencies as follows:

npm install @mui/material @emotion/react 
npm install @emotion/styled @mui/lab @mui/icons-material

Project Structure: The project structure should look like the below:

 

Steps to run the application: Run the project as follows:

npm start

Example 1: In the following example, we have a Paper component with different elevations.




import "./App.css";
import * as React from "react";
import Stack from "@mui/material/Stack";
import LinearProgress from "@mui/material/LinearProgress";
function App() {
    return (
        <div className="App">
            <div
                className="head"
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <h1
                    style={{
                        color: "green",
                    }}
                >
                    GeeksforGeeks
                </h1>
                <strong>
                    React MUI LinearProgress API
                </strong>
            </div>
            <br />
            <div
                style={{
                    display: "flex",
                    justifyContent: "space-evenly",
                    width: "50%",
                    margin: "auto",
                }}
            >
                <Stack sx={{
                    width: "100%",
                    color: "grey.500"
                }}
                    spacing={2}>
                    <LinearProgress color="secondary" />
                    <LinearProgress color="success" />
                    <LinearProgress color="inherit" />
                </Stack>
            </div>
        </div>
    );
}
  
export default App;

Output:

 

Example 2: In the following example, we have a determinate LinearProgress bar.




import "./App.css";
import * as React from "react";
import Stack from "@mui/material/Stack";
import LinearProgress from "@mui/material/LinearProgress";
function App() {
    const [progress, setProgress] =
        React.useState(0);
    React.useEffect(() => {
        const timer = setInterval(() => {
            setProgress((oldProgress) => {
                if (oldProgress === 100) {
                    return 0;
                }
                const diff = Math.random() * 10;
                return Math.min(oldProgress + diff,
                    100);
            });
        }, 500);
  
        return () => {
            clearInterval(timer);
        };
    }, []);
    return (
        <div className="App">
            <div
                className="head"
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <h1
                    style={{
                        color: "green",
                    }}
                >
                    GeeksforGeeks
                </h1>
                <strong>
                    React MUI LinearProgress API
                </strong>
            </div>
            <br />
            <div
                style={{
                    display: "flex",
                    justifyContent: "space-evenly",
                    width: "50%",
                    margin: "auto",
                }}
            >
                <Stack sx={{
                    width: "100%", color:
                        "grey.500"
                }}
                    spacing={2}>
                    <LinearProgress
                        variant="determinate"
                        value={progress}
                        color="secondary"
                    />
                </Stack>
            </div>
        </div>
    );
}
  
export default App;

Output:

 

Reference: https://mui.com/material-ui/api/linear-progress/


Article Tags :