How to change position of mui snackbar in react js?
November 04, 2023Hi Friends 👋,
Welcome To aGuideHub!
To change position of mui snackbar in react js, you can use anchorOrigin={{ vertical, horizontal }}
in snackbar. it will change position of mui snackbar in React JS.
Today, I am going to show you, How to change position 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 Box from "@mui/material/Box";
import Button from "@mui/material/Button";
import Snackbar from "@mui/material/Snackbar";
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
anchorOrigin={{ vertical, horizontal }}
open={open}
onClose={handleClose}
message="I love snacks"
key={vertical + horizontal}
/>
MUI material change position of mui snackbar example.
The below code is an example, you need to import snackbar alert
Component. Then, you can use anchorOrigin={{ vertical, horizontal }}
in snackbar. Then it will change position of mui snackbar in react js.
App.js
import * as React from "react";
import Box from "@mui/material/Box";
import Button from "@mui/material/Button";
import Snackbar from "@mui/material/Snackbar";
export default function PositionedSnackbar() {
const [state, setState] = React.useState({
open: false,
vertical: "top",
horizontal: "center"
});
const { vertical, horizontal, open } = state;
const handleClick = (newState) => () => {
setState({ ...newState, open: true });
};
const handleClose = () => {
setState({ ...state, open: false });
};
const buttons = (
<React.Fragment>
<Box sx={{ display: "flex", justifyContent: "center" }}>
<Button
onClick={handleClick({ vertical: "top", horizontal: "center" })}
>
Top-Center
</Button>
</Box>
</React.Fragment>
);
return (
<Box sx={{ width: 500 }}>
{buttons}
<Snackbar
anchorOrigin={{ vertical, horizontal }}
open={open}
onClose={handleClose}
message="I love snacks"
key={vertical + horizontal}
/>
</Box>
);
}
In the above code example, I have used the @mui/material
component and changed position of mui snackbar in react js.
Here, we are provided code sandbox links for the above program change position of mui snackbar in react js. Then you can use whenever you want and do the changes as per your requirements.
All the best 👍