Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement material-ui Snackbar as a global function?

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');

like image 509
Luciferous Avatar asked Nov 09 '20 15:11

Luciferous


People also ask

What is SnackBar in material UI?

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.

What is snackbar UI component?

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

How to configure the snackbar module?

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.

What is material snackbar design component?

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?

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


2 Answers

You have to do it in react way. You can achieve this by creating a Higher Order Component.

  1. Create a HOC that returns a snackbar component along with the wrappedComponent
  2. Create a function in that HOC which accepts message, severity (if you are using Alert like me), duration and sets the appropriate states which are set to the props of the snackbar. And pass that function as a prop to the wrappedComponent.
  3. Finally import this HOC wherever you want to display a snackbar, pass your component in it and call the HOC function from the prop (this.prop.functionName('Hello there!')) in the event handler where you want to display a snackbar and pass in a message.

Check this out. https://stackblitz.com/edit/snackbar-hoc?file=src/SnackbarHOC.js

like image 109
BeerendraMC Avatar answered Sep 30 '22 09:09

BeerendraMC


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.....'})
like image 42
Bogdan Iordachescu Avatar answered Sep 30 '22 10:09

Bogdan Iordachescu