[SalesForce] How to show error messages in lightning aura component

We have used ui:message tag to display error messages in lightning component. Lightning Ui namespace is deprecating soon hence we are migrating Ui namespace tags to the lightning namespace. Do we have any other alternate tag to display error messages in the component with close option. We need display around 32 error messages in the component.

<ui:message title="Error" severity="error" closable="true">
This is an error message.
</ui:message>

Thanks,
Anil Kumar

Best Answer

You want to use the Lightning Notifications Library. This has the capability to show toasts or notifications.

You can reference the documentation and examples here

Here is a basic example shown from documenation:

<aura:component>
    <lightning:notificationsLibrary aura:id="notifLib"/>
    <lightning:button name="notice" label="Show Notice" onclick="{!c.handleShowNotice}"/>
</aura:component>


({
    handleShowNotice : function(component, event, helper) {
        component.find('notifLib').showNotice({
            "variant": "error",
            "header": "Something has gone wrong!",
            "message": "Unfortunately, there was a problem updating the record.",
            closeCallback: function() {
                alert('You closed the alert!');
            }
        });
    }
})

And here is how you can show a toast

<aura:component>
    <lightning:notificationsLibrary aura:id="notifLib"/>
    <lightning:button name="toast" label="Show Toast" onclick="{!c.handleShowToast}"/>
</aura:component>

({
    handleShowToast : function(component, event, helper) {
        component.find('notifLib').showToast({
            "title": "Success!",
            "message": "The record has been updated successfully."
        });
    }
})

With the notification library, you have a much more robust system in place to show various messages to your user as it has tons of flexibility and options.