How to change autohideduration of mui snackbar in react js?
October 29, 2023Hi Friends 👋,
Welcome To aGuideHub!
To change autohideduration of mui snackbar in react js, you have to use autoHideDuration={2000}
component in snackbar alert. it will change autohideduration of mui snackbar in React JS.
textfield
Today, I am going to show you, How to change autohideduration of mui snackbar in react js
Installation
Install the following packages to use mui snackbar alert in react js.
npm
npm install @mui/material @emotion/react @emotion/styled
yarn
yarn add @mui/material @emotion/react @emotion/styled
Table of contents
- Install MUI and create a new React app.
- Import Material-UI snackbar alert.
- Use the snackbar alert Component
Step 1: Install MUI and create a new React app.
First you have to install the React project. You should use create-react-app
command to create a new React project.
npx create-react-app my-app
cd my-app
npm start
Step 2: Import Material-UI snackbar alert.
After installing MUI
, you have to import your React component. To do this, add the following line to the top of your component file.
import * as React from "react";
import Stack from "@mui/material/Stack";
import Button from "@mui/material/Button";
import Snackbar from "@mui/material/Snackbar";
import MuiAlert from "@mui/material/Alert";
Step 3: Use the snackbar alert Component.
A snackbar alert screen is an animated placeholder that simulates the layout of a website while data is being loaded.
<Snackbar open={open} autoHideDuration={2000} onClose={handleClose}>
<Alert onClose={handleClose} severity="success" sx={{ width: "100%" }}>
This is a success message!
</Alert>
</Snackbar>
MUI material change autohideduration of mui snackbar example.
The below code is an example, you need to import snackbar alert
Component. Then, you have to use autoHideDuration={2000}
component in snackbar alert. Then it will change autohideduration of mui snackbar in react js.
App.js
import * as React from "react";
import Stack from "@mui/material/Stack";
import Button from "@mui/material/Button";
import Snackbar from "@mui/material/Snackbar";
import MuiAlert from "@mui/material/Alert";
const Alert = React.forwardRef(function Alert(props, ref) {
return <MuiAlert elevation={6} ref={ref} variant="filled" {...props} />;
});
export default function CustomizedSnackbars() {
const [open, setOpen] = React.useState(false);
const handleClick = () => {
setOpen(true);
};
const handleClose = (event, reason) => {
if (reason === "clickaway") {
return;
}
setOpen(false);
};
return (
<Stack spacing={2} sx={{ width: "100%" }}>
<Button variant="outlined" onClick={handleClick}>
Open success snackbar
</Button>
<Snackbar open={open} autoHideDuration={2000} onClose={handleClose}>
<Alert onClose={handleClose} severity="success" sx={{ width: "100%" }}>
This is a success message!
</Alert>
</Snackbar>
</Stack>
);
}
In the above code example, I have used the @mui/material
component and change autohideduration of mui snackbar in react js.
Here, we are provided code sandbox links for the above program change autohideduration of mui snackbar in react js. Then you can use whenever you want and do the changes as per your requirements.
All the best 👍