Skip to content
Advertisement

How to dynamically create multiple alerts inside mui v5 Snackbar

I am calling an API to do few actions.
I want to take the response of each action and show it inside Snackbar/alert.

I am able to show only the first response and nothing else even after iterating the messages in a map.

Here is my business logic calling the api

JavaScript

setNotify will open the Snackbar with the alerts

JavaScript

The only issue it’s only displaying the first action and nothing else.

Edit On Sandbox

I suspect the alerts are overlapping on top of each other Maybe we need to add some sort of AutoGrow prop

Advertisement

Answer

You have to use notistack as described in the MUI doc:

This example demonstrates how to use notistack. notistack has an imperative API that makes it easy to display snackbars, without having to handle their open/close state. It also enables you to stack them on top of one another (although this is discouraged by the Material Design guidelines).

Start by wrapping your app inside a SnackbarProvider component then use useSnackbar hook to access enqueueSnackbar in order to add a snackbar to the queue to be displayed:

App.js

JavaScript

Notification.js

JavaScript

Demo:

Edit sparkling-moon-ikm0o

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement