Skip to content

FlashMessengerApi.addFlashMessages

FlashMessengerApi.addFlashMessages is an event on the Event Bus that occurs whenever a list of Flash Messages is being added to the Flash Messenger.

Event body

The FlashMessengerApi.addFlashMessages body has a TAddFlashMessagesEvent type which represents the flash message to add. It is optional so it might not exist.

Example

In this example we listen to FlashMessengerApi.addFlashMessages event and perform an action whenever it's emitted.

useStorefront(async (storefront) => {
    storefront.eventBus.on('FlashMessengerApi.addFlashMessages', ({ body }) => {
        console.log('Flash messages have been added:', body.flashMessages);
        console.log('To flash messenger of name:', body.containerName);
    });
});

Example

In this example we use a Message Storage API to retrieve an array of past FlashMessengerApi.addFlashMessages events.

useStorefront((storefront) => {
    const messageStorageApi = storefront.getApiSync('messageStorageSystemApi');

    const pastAddFlashMessagesEvents = messageStorageApi.getChannelMessages('FlashMessengerApi.addFlashMessages');

    if (pastAddFlashMessagesEvents.length > 0) {
        pastAddFlashMessagesEvents.forEach(({ body: eventBody }) => {
            console.log('perform action on every past event');
        });
    }
});

JS API reference