0.3.6 • Published 10 months ago

mui-simple-toast v0.3.6

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

mui-simple-toast - Simplified toaster/snackbar for MUI

A Simpler MUI Toast. This reduces code to single line compared to Actual Toast Code from MUI

<Snackbar open={open} autoHideDuration={6000} onClose={handleClose}>
  <Alert onClose={handleClose} severity="success" sx={{ width: '100%' }}>
    This is a success message!
  </Alert>
</Snackbar>

to

<MUISimpleToast toast={toast} setToast={setToast} />

Demo

If install fails, try running

npm i --legacy-peer-deps

How to use?

You can set toaster using seToast

Example on API Call:

fetch(url)
.then(() => setToast({
    show: true,
    type: 'success',
    message: 'Success!',
}))
.catch(err => setToast({
    show: true,
    type: 'error',
    message: err?.message || '',
    }))

In your component/Root component,

import React, { useEffect, useState } from 'react';
import MUISimpleToast, { defaultToast } from 'mui-simple-toast'
import './App.css';

function App() {

  const  [toast, setToast] = useState(defaultToast)

  useEffect(() => {
    setInterval(() => {
        setToast({
          show: true,
          type: 'success',
          message: 'Welcome to Simpler toast!',
        })
    }, 3000)
  }, [])

  return (
      <div className="App">
        <h1>Hi There. Can you see some toast?</h1>>
        <MUISimpleToast toast={toast} setToast={setToast} />
      </div>
  );
}

export default App;

In return method of component, add <MUISimpleToast toast={toast} setToast={setToast} />

Or you can use Context API

in index.js

import React, { createContext, useState } from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import './index.css';
import { defaultToast } from 'mui-simple-toast';

/** Create a Context for Toast Wrapper*/
export const ToastContext = createContext(defaultToast)
const ToastProvider = ({ children }: any) => {
  // ** States
  const [toast, updateToast] = useState(defaultToast)
  const setToast = (toastData: any) => updateToast({ ...toast, ...toastData })
  const values = {
    toast,
    setToast,
  }
  return <ToastContext.Provider value={values}>{children}</ToastContext.Provider>
}

root.render(
  <React.StrictMode>
    /** Wrap your Root component with Context Provider */
    <ToastProvider>
      <App />
    </ToastProvider>
  </React.StrictMode>
);

In your App/ Child Component, add

const { toast, setToast }: any = useContext(ToastContext)

and in return of same component,

<MUISimpleToast toast={toast} setToast={setToast} />
0.3.6

10 months ago

0.3.5

2 years ago

0.3.4

2 years ago

0.3.3

2 years ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.18

2 years ago

0.2.17

2 years ago

0.2.16

2 years ago

0.2.15

2 years ago

0.2.14

2 years ago

0.2.13

2 years ago

0.2.12

2 years ago

0.2.11

2 years ago

0.2.10

2 years ago

0.2.9

2 years ago

0.2.8

2 years ago

0.2.6

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago