โšก Create a Reusable Toast Utility in Salesforce LWC

Published on December 10, 2025 โ€ข by Anand Kumar Jha

๐Ÿ”น Introduction

Toast messages are one of the most commonly used UI elements in Lightning Web Components (LWC).

But repeating toast logic in every component leads to duplicate code. So today, weโ€™ll build a Reusable Toast Utility.

๐ŸŽฏ Goal

  • Create a universal toast helper function
  • Reduce duplicate code across LWCs
  • Trigger Success, Error, Warning, and Info toasts

๐Ÿ“ Folder Structure

lwc
 โ”œโ”€โ”€ toastUtils
 โ”œโ”€โ”€ toastDemo

๐Ÿงฉ Step 1: Create toastUtils Utility Module

import { ShowToastEvent } from "lightning/platformShowToastEvent";

const showToast = (cmp, title, message, variant) => {
  cmp.dispatchEvent(
    new ShowToastEvent({ title, message, variant })
  );
};

export { showToast };

๐Ÿ“บ Watch the Full Video

โ–ถ Watch on YouTube

๐Ÿงพ Conclusion

This reusable toast utility keeps your LWC code clean, modular, and scalable.

Share this article

Facebook | Twitter/X | LinkedIn | WhatsApp