Sub topic 1
Showing a toast with ShowToastEvent
Prerequisites
This chapter assumes you know the basic structure of a Lightning Web Component (HTML template, JavaScript class, and metadata file). It also assumes you can use @api and dispatchEvent. You should be comfortable with Salesforce records and objects.
What a toast is
A toast is a short message that appears at the top of the Lightning Experience page. It tells the user that something happened, such as a save succeeding or a call failing. A toast closes by itself or when the user dismisses it. It never blocks the page.
In Lightning Web Components, you don't render a toast yourself. You create a ShowToastEvent and dispatch it. The Lightning container catches the event and draws the toast.
The working model
The flow has three steps:
- Import
ShowToastEventfromlightning/platformShowToastEvent. - Create an instance with the options that describe the toast.
- Pass the instance to
this.dispatchEvent.
A first toast
import { LightningElement } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
export default class ToastDemo extends LightningElement {
handleSave() {
this.dispatchEvent(
new ShowToastEvent({
title: 'Success',
message: 'The contact was saved.',
variant: 'success',
mode: 'dismissible'
})
);
}
}The constructor takes one object. These are the main properties:
| Property | Purpose |
|---|---|
| `title` | The bold heading of the toast. |
| `message` | The body text. |
| `variant` | The style: `info`, `success`, `warning`, or `error`. |
| `mode` | How long the toast stays: `dismissible`, `pester`, or `sticky`. |
The next chapter covers variants and modes in detail. For now, variant: 'success' gives a green toast with a check icon, and mode: 'dismissible' makes it close on its own after a few seconds or when the user clicks the close button.
Common errors
- Dispatching from the wrong object. Call
this.dispatchEventinside the component class. If you call it from a plain helper function,thisis not your component and nothing appears. - Importing from the wrong module. The correct path is
lightning/platformShowToastEvent. A typo in the path stops the component from compiling. - Expecting a toast outside Lightning Experience. The container that displays toasts is part of the Lightning page. In some other contexts, such as a component hosted outside Salesforce, the event may have no visible effect.
- Forgetting that the toast is fire and forget.
dispatchEventreturns right away. You get no callback when the user dismisses the toast, so don't build logic that depends on it.
Using it after an async call
Toasts are most useful after an operation finishes. Dispatch the event inside the success or failure branch, not before the call:
import { LightningElement } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
export default class SaveWithToast extends LightningElement {
async handleSave() {
try {
await this.saveContact();
this.showToast('Success', 'The contact was saved.', 'success');
} catch (error) {
this.showToast('Save failed', error.message, 'error');
}
}
async saveContact() {
// Replace with a real call, for example an imperative Apex method.
return Promise.resolve();
}
showToast(title, message, variant) {
this.dispatchEvent(new ShowToastEvent({ title, message, variant }));
}
}Exercise
A developer calls new ShowToastEvent({ title: 'Saved' }) but never passes the result to anything, and no toast appears. What is missing?
Show answer
The event must be dispatched. Calling the constructor only creates the event object. The component must call this.dispatchEvent(...) with it so the Lightning container receives it.