timolins / react-hot-toast

Smoking Hot React Notifications 🔥
https://react-hot-toast.com
MIT License
9.4k stars 309 forks source link

Dismiss and duration don't work on a custom toast #199

Open benjamin-benoit opened 2 years ago

benjamin-benoit commented 2 years ago

Hello, I did a custom toast using a component:

toast.custom(<ScheduledToaster />);

But I can't use the duration like that:

toast.custom(<ScheduledToaster />, { duration: 10000 });

And I can't use toast.dismiss(), to remove all custom toasts, only toast.remove() is working.

peniqliotuv commented 2 years ago

+1 to this - only calling toast.remove() and toast.remove(id) is working for me as well -- I'm using toast() instead of toast.custom() though

timolins commented 1 year ago

Is it possible that it works, but is delayed for 1 second? This is because toast.dismiss() doesn't remove the toast instantly. It waits for one second in order to leave room an unmount animation. toast.custom() currently doesn't apply any animations – #116.

You can react to t.visible to hide it yourself.

Seems to be related to #173

benjamin-benoit commented 1 year ago

@timolins It never disappear for me...

without doing a custom toast toast(<ScheduledToaster cart={cart} />, { duration: 10000 });

It work, but I see a empty toast upper of mine...

timolins commented 1 year ago

Can you please provide an example which reproduces this issue? It works fine on my end: https://codesandbox.io/s/react-hot-toast-custom-toast-dismiss-re8xhs

AngheloGc commented 1 year ago

Hi. I also ran into this issue. Here I have replicated the case: https://codesandbox.io/s/react-hot-toast-dismiss-and-duration-bug-krbt1p?file=/src/TestView.tsx

brandonpittman commented 1 year ago

I'm doing something like this with Framer Motion and it's working well.

Toast caller:

  useEffect(() => {
    if (message) {
      toast.custom(
        (t) => (
          <Notification
            isVisible={t.visible}
            onDismiss={() => toast.dismiss(t.id)}
          >
            {message}
          </Notification>
        ),
        { duration: 20000 }
      );
    }
  }, [message]);

Notification component:

<AnimatePresence>
  {isVisible && (
    <motion.div
      key="notification"
      initial={initial}
      animate={animate}
      exit={exit}
      role="alert"
    >
  </motion.div
</AnimatePresence>
brandonpittman commented 1 year ago

Can you please provide an example which reproduces this issue? It works fine on my end: https://codesandbox.io/s/react-hot-toast-custom-toast-dismiss-re8xhs

In this example, because t.visible starts as true, the enter transition does nothing. It just pops into existence. The exit transition does work because RHT waits 1 second before removing the element from the DOM.

seanmcquaid commented 11 months ago

Also seeing this as well with custom toasts. For now, I am just using a useEffect in the custom component to clear the toast.

deepaktammali commented 8 months ago

We are facing this issue as well with custom toasts.

kylegillen commented 4 months ago

In my case, adding type="button" to the button element that was ultimately calling () => toast.dismiss(t.id) fixed the issue 🤷‍♂️

felipemullen commented 1 week ago

There is definitely something strange happening on custom toasts. In my case, calling toast.custom causes the popup to occasionally hang unless I change to another tab or mouse over the browser's inspector window:

toast.custom((t) => <PlayerWinsHand winningPlayer={player} toast={t} />, { duration: 1250 });

I have to manually dismiss using toast.dismiss