dispatchBrowserEvent on mount

Blockquote
You cannot send events from the mount method. Think of it like the constructor. If you want something to appear in the view you must make it a public property, pass it directly to the rendered view or put it in session and pull it from there.

Thanks @Snapey, this is the explanation I was looking for :slight_smile: Thanks to both you and @skywalker for all your help, I appreciate it! I will try to find another way to display a notification.

There is a way to post a notification after redirect, even reusing the solution from the screencast. In the blade where you placed <x-notification/> add following code:

@if(session()->has('notify'))
<script>
    window.onload = function() {
        window.dispatchEvent(new CustomEvent('notify', {
            detail: '{{ session("notify") }}'
        }));
    }
</script>
@endif

If the session has notify, the script will be injected and dispatch the notify browser event when page has loaded. This event will be recogized by the notification component. Of course you still have to flash the message in your component.

2 Likes

Exactly what I was looking for as it allows me to use same page notifications in some places and flash to session when Iā€™m redirecting. Thanks!