FlashMessengerApi.addFlashMessage¶
FlashMessengerApi.addFlashMessage is an event on the Event Bus that occurs whenever a Flash Message is being added to the Flash Messenger.
Event body¶
The FlashMessengerApi.addFlashMessage body has a TAddFlashMessageEvent type which represents the flash message to add. It is optional so it might not exist.
Example¶
In this example we listen to FlashMessengerApi.addFlashMessage event and perform an action whenever it's emitted.
useStorefront(async (storefront) => {
storefront.eventBus.on('FlashMessengerApi.addFlashMessage', ({ body }) => {
console.log('Flash message has been added:', body.flashMessage);
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.addFlashMessage events.
useStorefront((storefront) => {
const messageStorageApi = storefront.getApiSync('messageStorageSystemApi');
const pastAddFlashMessageEvents = messageStorageApi.getChannelMessages('FlashMessengerApi.addFlashMessage');
if (pastAddFlashMessageEvents.length > 0) {
pastAddFlashMessageEvents.forEach(({ body: eventBody }) => {
console.log('perform action on every past event');
});
}
});