react-hot-toast icon indicating copy to clipboard operation
react-hot-toast copied to clipboard

bug: toast remains in the DOM if call programmatically.

Open weeebdev opened this issue 2 years ago • 4 comments

I was writing tests for my custom toast wrapped around react-hot-toast and faced with the bug where my toast persisted at the DOM tree. I decided to try the original lib and it happened again.

Just try:

import { useEffect } from "react";
import toast, { Toaster } from "react-hot-toast";

function App() {
  useEffect(() => {
    for (let index = 0; index < 10; index++) {
      toast.success(index + "");
    }
  }, []);

  return (
    <div className="App">
      <Toaster />
      <button onClick={() => toast.success("Hello!")}>Click!</button>
    </div>
  );
}

export default App;

image

Same happens if I click on the button very fast, some toasts remain in the DOM.

Is there a way to fix it? Will be very glad, if there is a workaround

weeebdev avatar Jul 21 '21 14:07 weeebdev

Maybe this can help:

import { useEffect } from "react";
import toast, { Toaster, useToaster } from "react-hot-toast";

export default function App() {
  const { toasts } = useToaster();

  useEffect(() => {
    // this line will ensure that all toasts are remove from the DOM when their duration expires.
    toasts.forEach((item) => {
      if (!item.visible) {
        toast.remove(item.id);
      }
    });
  }, [toasts]);

  useEffect(() => {
    for (let index = 0; index < 10; index++) {
      toast.success(index + "");
    }
  }, []);

  return (
    <div className="App">
      <Toaster />
      <button onClick={() => toast.success('Hello!')}>Click!</button>
    </div>
  );
}

alexandredev3 avatar Jul 26 '21 03:07 alexandredev3

Interesting, thanks for reporting. I'll have a closer look.

timolins avatar Jul 26 '21 11:07 timolins

Bump.

bruce-c-liu avatar Nov 10 '22 00:11 bruce-c-liu

Fixed by https://github.com/timolins/react-hot-toast/pull/251. @timolins can we get this merged?

cmmartin avatar Apr 25 '23 17:04 cmmartin