Introduction

In this article, we will discuss React Material UI Progress bar and Snackbar. Progress bar is used to inform the user that data loading is in progress. Basically it is used to show the loading state of data in an application.
You can check my previous articles in which we discussed ReactJS and its basic components from the below-mentioned links.
Prerequisites

Create ReactJS project

Let's first create a React application using the following command.
  1. npx create-react-app platform

Install Material-UI

Now Install Material-UI by using the following command.
  1. npm install @material-ui/core --save
Now, right-click on the "src" folder and add a new component named 'Progressdemo.js'
Add the following code in Progressdemo component.
  1. import React from 'react';
  2. import clsx from 'clsx';
  3. import { makeStyles } from '@material-ui/core/styles';
  4. import CircularProgress from '@material-ui/core/CircularProgress';
  5. import { green } from '@material-ui/core/colors';
  6. import Button from '@material-ui/core/Button';
  7. import AppBar from '@material-ui/core/AppBar';
  8. import Toolbar from '@material-ui/core/Toolbar';
  9. const useStyles = makeStyles(theme => ({
  10. buttonSuccess: {
  11. backgroundColor: green[500],
  12. '&:hover': {
  13. backgroundColor: green[700],
  14. },
  15. },
  16. buttonProgress: {
  17. color: green[500],
  18. position: 'absolute',
  19. top: '50%',
  20. left: '50%',
  21. marginTop: -12,
  22. marginLeft: -12,
  23. },
  24. }));
  25. export default function Progessdemo() {
  26. const classes = useStyles();
  27. const [loading, setLoading] = React.useState(false);
  28. const [success, setSuccess] = React.useState(false);
  29. const timer = React.useRef();
  30. const buttonClassname = clsx({
  31. [classes.buttonSuccess]: success,
  32. });
  33. const handleButtonClick = () => {
  34. if (!loading) {
  35. setSuccess(false);
  36. setLoading(true);
  37. timer.current = setTimeout(() => {
  38. setSuccess(true);
  39. setLoading(false);
  40. }, 2000);
  41. }
  42. };
  43. return (
  44. <>
  45. <AppBar position="static">
  46. <Toolbar style={{ 'paddingLeft': "600px" }}>
  47. Material UI Progres Bar
  48. </Toolbar>
  49. </AppBar>
  50. <div>
  51. <div style={{ 'paddingLeft': "600px" }}>
  52. {loading && <CircularProgress size={80} className={classes.buttonProgress} />}
  53. </div>
  54. <div style={{ 'paddingLeft': "600px" }}>
  55. <Button
  56. variant="contained"
  57. color="primary"
  58. className={buttonClassname}
  59. disabled={loading}
  60. onClick={handleButtonClick}
  61. >
  62. Click
  63. </Button>
  64. </div>
  65. </div>
  66. </>
  67. );
  68. }
Run the project by using 'npm start' and check the result:
Now, right-click on the "src" folder and add a new component named 'Progressdemo1.js' and add the following code int this component
  1. import React from 'react';
  2. import { makeStyles } from '@material-ui/core/styles';
  3. import LinearProgress from '@material-ui/core/LinearProgress';
  4. import AppBar from '@material-ui/core/AppBar';
  5. import Toolbar from '@material-ui/core/Toolbar';
  6. export default function Progessdemo1() {
  7. return (
  8. <>
  9. <div style={{"marginBottom":"20px"}}>
  10. <AppBar position="static">
  11. <Toolbar style={{ 'paddingLeft': "600px" }}>
  12. Material UI Progres Bar
  13. </Toolbar>
  14. </AppBar>
  15. </div>
  16. <div>
  17. <LinearProgress />
  18. <LinearProgress color="secondary" />
  19. </div>
  20. </>
  21. );
  22. }

Run the project by using 'npm start' and check the result.

Now, right-click on the "src" folder and add a new component named 'Snackbar.js' and add the following code int this component
  1. import React from 'react';
  2. import Button from '@material-ui/core/Button';
  3. import Snackbar from '@material-ui/core/Snackbar';
  4. import AppBar from '@material-ui/core/AppBar';
  5. import Toolbar from '@material-ui/core/Toolbar';
  6. export default function SnackbarDemo() {
  7. const [state, setState] = React.useState({
  8. open: false,
  9. vertical: 'top',
  10. horizontal: 'center',
  11. });
  12. const { vertical, horizontal, open } = state;
  13. const handleClick = newState => () => {
  14. setState({ open: true, ...newState });
  15. };
  16. const handleClose = () => {
  17. setState({ ...state, open: false });
  18. };
  19. return (
  20. <div>
  21. <AppBar position="static">
  22. <Toolbar style={{ 'paddingLeft': "600px" }}>
  23. Material UI Snack Bar
  24. </Toolbar>
  25. </AppBar>
  26. <Button onClick={handleClick({ vertical: 'bottom', horizontal: 'center' })}>
  27. Bottom-Center
  28. </Button>
  29. <Button onClick={handleClick({ vertical: 'bottom', horizontal: 'left' })}>Bottom-Left</Button>
  30. <Button onClick={handleClick({ vertical: 'top', horizontal: 'left' })}>Top-Left</Button>
  31. <Snackbar
  32. anchorOrigin={{ vertical, horizontal }}
  33. key={`${vertical},${horizontal}`}
  34. open={open}
  35. onClose={handleClose}
  36. message="Snack Bar Demo"
  37. />
  38. </div>
  39. );
  40. }
Open app.js file and add the following code:
  1. import React from 'react';
  2. import logo from './logo.svg';
  3. import './App.css';
  4. import SnackbarDemo from './Snackbar';
  5. function App() {
  6. return (
  7. <div className="App">
  8. <SnackbarDemo/>
  9. </div>
  10. );
  11. }
  12. export default App;
Run the project by using 'npm start' and check the result:

Summary

In this article, we learned how we add Progressbar and Snackbar in React applications using React material UI.