unovue / shadcn-vue

Vue port of shadcn-ui
https://www.shadcn-vue.com/
MIT License
4.8k stars 275 forks source link

Toast Error in VILT Stack Navigating to a page from other page while the toast was still open [Bug]: #666

Closed nehry-micto closed 2 months ago

nehry-micto commented 3 months ago

Reproduction

n/a

Describe the bug

Calling a toast then navigating to other page while the toast was still open giving an error. I think it was because the toast was still there but the parent component was unmounted? I'm using Laravel, Inertia, Vue and TailwindCSS. Is there a work around for this?

This is the AuthenticationLayout.vue where I added toast component Screenshot 2024-07-18 090535 The error I encountered while the toast was open, then navigating to other page. Screenshot 2024-07-18 085923 Screenshot 2024-07-18 090020

System Info

Brave Browser

Contributes

seergiue commented 3 months ago

Same issue here

sadeghbarati commented 2 months ago

Can you guys provide a minimal reproduction with Stackblitz?

nehry-micto commented 2 months ago

I have created a temporary solution, I added an event handler from inertia https://inertiajs.com/events, it listen if you navigated to other page, then I added a function that calling the dispatch to remove the toast.

use-toast.js

function removeAllToast() {
    dispatch({ type: actionTypes.REMOVE_TOAST });
}
export { toast, useToast, removeAllToast };

AuthLayout.vue

let removeStartEventListener = router.on("start", (event) => {
    removeAllToast();
});

onUnmounted(() => {
    removeStartEventListener();
});
plymth commented 2 months ago

@nehry-micto you can also do the following and use the useToast().dismiss() so you don't have to create an additoinal function.

let removeStartEventListener = router.on("start", (event) => {
  useToast().dismiss()
});

onUnmounted(() => {
    removeStartEventListener();
});
sadeghbarati commented 2 months ago

<Toaster> component must be used in app-wide components like layouts, it should not be placed in pages/routes components

stefanocurnis commented 1 month ago

<Toaster> component must be used in app-wide components like layouts, it should not be placed in pages/routes components

I have it in my Layout, but with Inertia layouts are unmounted and remounted everytime you click.