Material UI is a Material Design library made for React. It’s a set of React components that have Material Design styles. In this article, we’ll look at how to customize snack bars with Material UI.
Material UI is a Material Design library made for React.
It’s a set of React components that have Material Design styles.
In this article, we’ll look at how to customize snack bars with Material UI.
We can display multiple snack bars consecutively with some logic.
For example, we can write:
import React from "react";
import Button from "@material-ui/core/Button";
import Snackbar from "@material-ui/core/Snackbar";
import IconButton from "@material-ui/core/IconButton";
import CloseIcon from "@material-ui/icons/Close";
export default function App() {
const [snackPack, setSnackPack] = React.useState([]);
const [open, setOpen] = React.useState(false);
const [messageInfo, setMessageInfo] = React.useState(undefined);
React.useEffect(() => {
if (snackPack.length && !messageInfo) {
setMessageInfo({ ...snackPack[0] });
setSnackPack(prev => prev.slice(1));
setOpen(true);
} else if (snackPack.length && messageInfo && open) {
setOpen(false);
}
}, [snackPack, messageInfo, open]);
const handleClick = message => () => {
setSnackPack(prev => [...prev, { message, key: new Date().getTime() }]);
};
const handleClose = (event, reason) => {
if (reason === "clickaway") {
return;
}
setOpen(false);
};
const handleExited = () => {
setMessageInfo(undefined);
};
return (
<div>
<Button onClick={handleClick("Message A")}>Show A</Button>
<Button onClick={handleClick("Message B")}>Show B</Button>
<Snackbar
open={open}
autoHideDuration={6000}
onClose={handleClose}
onExited={handleExited}
message={messageInfo ? messageInfo.message : undefined}
action={
<React.Fragment>
<Button color="secondary" size="small" onClick={handleClose}>
foo
</Button>
<IconButton color="inherit" onClick={handleClose}>
<CloseIcon />
</IconButton>
</React.Fragment>
}
/>
</div>
);
}
The snackPack
state has the items to display.
We set the message with setMessageInfo
.
This is done in the useEffect
callback.
When a button is clicked, then the old message is removed from the snackPack
with setSnackPack
and then we add the new one.
The handleClick
function lets us change the message
.
Now when we click the button, then old is gone and the new one is displayed.
technology javascript software-development web-development programming
To make the most out of the benefits of offshore software development, you should understand the crucial factors that affect offshore development.
Software Developer vs Web Developer | Difference Web Developer & Software Developer | Software development and web development are normally used interchangeably. Although both include development, a software developer has different responsibilities from that of a web developer.
Vinew Technologies focused majorly over ERP Software services and POS Software services, Web & Application development services & Woo commerce and Wordpress easy-to-use, multipurpose social media plugin for WordPress.
Check out these five criteria for the selection of your software vendor, and you will never regret having the wrong quality product made for you.
Mobiweb Technologies is a prestigious IT company headquartered in Indore providing promising and cost effective software and web development services across the globe.