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

How to avoid having multiple toasts on screen?

Open siyao-polarr opened this issue 1 year ago • 3 comments

Is there an option to have a new toast replace the current toast instead of adding on?

siyao-polarr avatar Jun 06 '23 22:06 siyao-polarr

https://react-hot-toast.com/docs/toast

See the "prevent duplicate toasts" section

pandeymangg avatar Jun 09 '23 05:06 pandeymangg

to limit the number of toasts shown on screen, customize the toaster component like

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

const ToasterComponent = () => {
    const { toasts } = useToasterStore();
    const TOAST_LIMIT = 1;

    useEffect(() => {
        toasts
            .filter((t) => t.visible) // Only consider visible toasts
            .filter((_, i) => i >= TOAST_LIMIT) // Is toast index over limit?
            .forEach((t) => toast.dismiss(t.id)); // Dismiss – Use toast.remove(t.id) for no exit animation
    }, [toasts]);

    return (<Toaster maxCount={1} />)
};

export default ToasterComponent

Hope this helps

camkon avatar Jun 18 '23 02:06 camkon

Just call toast.dismiss() before toast.success or toast.error.

myNameIsHamza avatar Aug 04 '24 19:08 myNameIsHamza