I am creating my react app with material-ui Snackbar. In my project I have a lot of components and don't want to insert <Snackbar/> in each of them. Is there a way to create function that will show snackbar, then just import and use this function in each component?
Something like:
import showSnackbar from 'SnackbarUtils';
showSnackbar('Success message');
Snackbars inform users of a process that an app has performed or will perform. They appear temporarily, towards the bottom of the screen. They shouldn't interrupt the user experience, and they don't require user input to disappear.
Snackbar is an important UI element in designing frontend applications.It is a small popup window, displays reactive information messages to accept user input from a user. This can be dismissed with user action either swapping or user click
The following are configuration parameters that are being supplied to the snack bar component. MatSnackBarModule module provides snackbar related functional components. To use these components, Import them into your application module as follows. Now imported in app.module.ts, all components of the module are available across all your components.
Important points about Material Snackbar Design component It shows a notification message popup at bottom of the screen by default It does not interrupt the user experience on the current page The recommendation is only one snack bar shown at a time on a page In android, the user notified a snack bar message for the below use cases
Is there a way to create function that will show snackbar, then just import and use this function in each component? You have to do it in react way. You can achieve this by creating a Higher Order Component. Create a HOC that returns a snackbar component along with the wrappedComponent
You have to do it in react way. You can achieve this by creating a Higher Order Component.
Check this out. https://stackblitz.com/edit/snackbar-hoc?file=src/SnackbarHOC.js
extend it as a Hook, and then you can call it once and use state with effects to show:
import { useSnackbar } from 'notistack';
import IconButton from "@mui/material/IconButton";
import CloseIcon from "@mui/material/SvgIcon/SvgIcon";
import React, {Fragment, useEffect, useState} from "react";
const useNotification = () => {
const [conf, setConf] = useState({});
const { enqueueSnackbar, closeSnackbar } = useSnackbar();
const action = key => (
<Fragment>
<IconButton onClick={() => { closeSnackbar(key) }}>
<CloseIcon />
</IconButton>
</Fragment>
);
useEffect(()=>{
if(conf?.msg){
let variant = 'info';
if(conf.variant){
variant = conf.variant;
}
enqueueSnackbar(conf.msg, {
variant: variant,
autoHideDuration: 5000,
action
});
}
},[conf]);
return [conf, setConf];
};
export default useNotification;
Then you can use it:
const [msg, sendNotification] = useNotification();
sendNotification({msg: 'yourmessage', variant: 'error/info.....'})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With