Lightning Web Components – Displaying Toast Message Only Once After Login on Home Page

Is it possible to display toast message notification only once when user login to system on a home page?

I don't want toast message appear every time if I navigate to home page multiple time?

Wanted to know if its possible in LWC or AURA?

Best Answer

Yes, we can achieve this. We have to do these steps:

  1. Create a LWC/Aura component that can show toast message.
  2. The Aura component will use localStorage to track whether it's first time if yes then we will show the toast and update in localStorage.
  3. And if it's not first time then we simply skip the toast message.
  4. This component will not have any UI elements.
  5. Now drop the component on the home page using edit page.
  6. Logout and login again and you should see toast when you visit the page first time.

Here is the sample code of AURA:

var isFirstView = localStorage.getItem('isFirstView') || '';
if (isFirstView !== 'Yes') {
    /* Show message to use as this is first view. */
    localStorage.setItem('isFirstView', 'Yes');
}

In my requirement I wanted to use local storage.

Related Topic