notistack

repository·master·Indexed 26 days ago

https://github.com/iamhosseindhv/notistack

A React library for displaying highly customizable notification snackbars (toasts) that can be stacked, queued, and triggered via function calls. It provides a SnackbarProvider for application wrapping, a useSnackbar hook for functional components, and an enqueueSnackbar function for triggering notifications from outside the React component tree. Version 3.0.2 is standalone, while earlier versions maintain peer dependencies on Material-UI.

Tokens
3.5K
Snippets
8
Records
25
Agent score
73%

What's inside notistack

  1. Replace withSnackbar HOC with useSnackbar or direct imports

    master

    The withSnackbar Higher-order component (HOC) has been removed. To migrate, you have two options:

    1. Functional Components: Convert your component to a functional component and use the useSnackbar hook.
    2. Direct Imports: Remove the HOC and import enqueueSnackbar or closeSnackbar directly from notistack to use them within your component logic.
    // Before
    import { withSnackbar } from 'notistack' 
    
    class MyButton extends React.Component {
        render() {
           const { enqueueSnackbar } = this.props
        }
    }
    export default withSnackbar(MyButton)
    
    // After (Option 2: Direct Imports)
    import { enqueueSnackbar, closeSnackbar } from 'notistack' 
    
    class MyButton extends React.Component {
        render() {
            // Use enqueueSnackbar directly from the import
        }
    }
    export default MyButton
  2. Migrate HTML attributes to SnackbarProps

    master

    In v2, HTML attributes (like data-* attributes) applied to the Snackbar root component must now be passed through the SnackbarProps object instead of being passed directly to the provider or the enqueue function. This applies to both the SnackbarProvider configuration and individual enqueueSnackbar calls.

    // Provider configuration
    <SnackbarProvider
        SnackbarProps={{
            'data-test': 'test',
        }}
    >
    
    // Individual snackbar call
    enqueueSnackbar('message', {
        SnackbarProps: {
           'data-test': 'test',
        },
    })
  3. Choose the correct version based on Material-UI

    master

    Depending on your project's Material-UI version, you may need to install a specific version of notistack to satisfy peer dependency requirements:

    • v3.x.x: Latest stable release. It is standalone and does not depend on Material-UI.
    • <= v2.0.8: Requires Material-UI v5 as a peer dependency. Install via npm install notistack@2.0.8.
    • <= 1.0.10: Requires Material-UI <= v4 as a peer dependency. Install via npm install notistack@latest-mui-v4.
  4. View the Notistack Redux example on CodeSandbox

    master

    You can explore a live implementation of Notistack integrated with Redux using the CodeSandbox demo. This example demonstrates how to trigger snackbar notifications within a Redux-managed application state.

    https://codesandbox.io/s/github/iamhosseindhv/notistack/tree/master/examples/redux-example
  5. Display snackbars using the useSnackbar hook

    master

    For a more idiomatic React approach, use the useSnackbar hook. Note that the component calling the hook must be a child of the SnackbarProvider to access the context.

    import { SnackbarProvider, useSnackbar } from 'notistack';
    
    // wrap your app
    <SnackbarProvider>
      <App />
      <MyButton />
    </SnackbarProvider>
    
    const MyButton = () => {
      const { enqueueSnackbar, closeSnackbar } = useSnackbar();
      return <Button onClick={() => enqueueSnackbar('I love hooks')}>Show snackbar</Button>;
    };
  6. Display snackbars using enqueueSnackbar

    master

    To show notifications, wrap your application in a SnackbarProvider and use the enqueueSnackbar function. This approach is useful for triggering notifications from outside the React component tree or in simple setups.

    import { SnackbarProvider, enqueueSnackbar } from 'notistack';
    
    const App = () => {
      return (
        <div>
          <SnackbarProvider />
          <button onClick={() => enqueueSnackbar('That was easy!')}>Show snackbar</button>
        </div>
      );
    };
  7. Define custom snackbar variants using the Components prop

    master

    To create entirely customized snackbars or new variants, use the Components prop on the SnackbarProvider. Your custom component will receive all standard Notistack props (like variant and message) as well as any additional custom options passed via enqueueSnackbar.

    <SnackbarProvider
        Components={{
            success: MyCustomSuccessNotification,
            reportComplete: ReportComplete,
        }}
    >
    </SnackbarProvider>
    
    interface ReportCompleteProps extends CustomContentProps {
        allowDownload: boolean;
    }
    
    const ReportComplete = React.forwardRef((props: ReportCompleteProps, ref) => {
        const {
            variant,
            message,
            allowDownload, // Custom prop passed via enqueueSnackbar
        } = props;
        // ...
    });
    
    // Triggering the custom variant with custom props
    enqueueSnackbar('Your report is ready to download', {
       variant: 'reportComplete',
       persist: true,
       allowDownload: true,
    })
  8. Configure OptionsObject for snackbars

    master

    When calling enqueueSnackbar, you can pass an OptionsObject to customize the snackbar's behavior.

    Key options:

    • variant: The visual style of the snackbar. Supported values: 'default' | 'error' | 'success' | 'warning' | 'info'.
    • anchorOrigin: Position of the snackbar. Format: { vertical: 'top' | 'bottom', horizontal: 'left' | 'center' | 'right' }.
    • autoHideDuration: Time in milliseconds before the snackbar closes automatically (default: 5000). Set to null to disable.
    • persist: If true, the snackbar stays on screen until manually dismissed (default: false).
    • preventDuplicate: If true, ignores requests to show a snackbar with the same message (default: false).
    • action: A SnackbarAction (React node or a function returning a node) to display buttons in the snackbar.
    • transitionDuration: Customizes transition timing. Can be a single number (ms) or an object: { enter: number, exit: number }.
  9. Review breaking changes in v2

    master

    When upgrading to v2, be aware of the following removals and deprecations:

    • Deprecated: The content prop is deprecated and will be removed in future releases. Use the Components prop for custom content instead.
    • Removed: ariaAttributes prop (use custom components for aria-attributes).
    • Removed: Transition callbacks resumeHideDuration, onEntering, and onExisting.
    • Removed: onClose no longer returns reason: 'clickaway'.
    • Removed: Customization via classes.variant(Success|Error|Info|Warning). Use the Components prop for variant-specific styling.
    • Theme Limitation: Material-UI theme overrides (including Dark/Light mode toggling) are not automatically applied to snackbar elements. Use a custom component to ensure snackbars react to theme changes.