# How can developers display user notification banners using the ShowToastEvent module?

**Topic:** LWC · **Difficulty:** Medium · **Tags:** LWC, Toast Notifications, User Feedback

Read it on the site: https://www.forcechallenges.com/interview-questions/how-can-developers-display-user-notification-banners-using-the-showtoastevent-module

## Answer

**Basics:** A toast is a temporary banner that confirms an action or reports a problem without navigating away. It is the standard way to give feedback after a save, delete or failed callout.

**How it works:**
1. `import { ShowToastEvent } from 'lightning/platformShowToastEvent';`.
2. Build the event with properties: `title` (bold header), `message` (body), `variant` (`info`, `success`, `warning`, `error`) and `mode` (`dismissible`, `pester`, `sticky`).
3. Dispatch it from the component: `this.dispatchEvent(evt)`. The container (Lightning page) renders it.
4. `messageData` lets you add links: `message: 'Created {0}'`, `messageData: [{ url: '/' + id, label: 'record' }]`.

**Advanced / deep dive:** Modes: `dismissible` stays until the user closes it or about 3 seconds pass; `pester` stays about 3 seconds with no close button; `sticky` stays until the user closes it. Use `sticky` for errors needing attention. Toast support depends on the container: it works in Lightning Experience, the Salesforce mobile app and Experience Builder sites in supported setups, but not in Lightning Out or standalone apps, so verify for your runtime and for LWR sites. Toasts are for feedback, not validation; use `lightning-input` `setCustomValidity` and `reportValidity` for field errors, or `lightning-messages` in forms. Keep text concise and localize via Custom Labels (`@salesforce/label/c.Label_Name`). For accessibility, the toast uses a live region, but avoid relying on it alone for critical errors; show persistent inline messages as well. Flows have their own toast action and Aura has `force:showToast`.

**Code / config example:**
```javascript
import { LightningElement } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import SAVED from '@salesforce/label/c.Record_Saved';

export default class Notifier extends LightningElement {
    notify(recordId) {
        this.dispatchEvent(new ShowToastEvent({
            title: SAVED,
            message: 'Open {0}',
            messageData: [{ url: `/${recordId}`, label: 'the record' }],
            variant: 'success',
            mode: 'dismissible'
        }));
    }
}
```

**Hidden gotchas & must-remember points:**
- The classic module is `lightning/platformShowToastEvent`; a newer `lightning/toast` module also exists, so verify in current release notes which fits your context.
- Toasts do nothing outside a supporting container (for example Lightning Out or LWC Open Source).
- Use `variant: 'error'` with `mode: 'sticky'` for errors users must read.
- Message text is plain text; `messageData` links are the way to include a hyperlink.
- A component inside a modal still dispatches normally, but timing may differ on mobile.
- Do not rely on toasts as the only record of a failure; log it.

**Best practice:** Toast for transient outcomes, inline messages for validation, and Custom Labels for text.

---
Source: ForceChallenges · last updated 2026-11-02
