-
Notifications
You must be signed in to change notification settings - Fork 340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
limit total number of toasts #31
Comments
That sounds like a reasonable improvement that shouldn't be too hard to implement. Consider this as on the road map. |
Hey @timolins. I have been looking at some open source projects to contribute to, and this looks like one awesome project I am willing to work on. Would you mind having me look at this issue? |
I'm also find this useful, since show multiple toast in a time a bit not needed Is there any way how to do this with current version? At least without using |
You can achieve by using const { toasts } = useToasterStore();
const TOAST_LIMIT = 3
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]); Check out this CodeSandbox example. |
I think the proposed solution works well enough – no need to add this as native API IMO. |
We wanted to extend it in a corp ui-kit. Could you add a possibility to change react-hot-toast/src/core/store.ts Line 4 in 0d417d9
|
According to the demand, it's time for a proper API. Will look into this for the next release. |
Thanks @timolins! I'd be happy to write a PR for this if you'd be open to that? |
Any news? |
Anything yet @timolins? |
In the meantime, I used @timolins
Still, hopefully it'll be part of the native API soon |
@LuisEgan Yeah, I've used a similar solution as I wanted to set a max limit on mobile devices. Hoping to have an official API soon. |
I created a custom toaster component based on previous comments, ` const TOAST_LIMIT = 10; useEffect(() => { return } |
Any Update guys on this ?? |
Any updates on this? |
There's kind of a hack using css - Give a containerClassName in Toaster -
And simply put this in your css -
and you can use nth-child concept to show the number of toast you want. |
Any updates? |
You can create a custom hook to do that
…On Wed, 26 Apr 2023, 11:36 am Łukasz, ***@***.***> wrote:
Any updates?
—
Reply to this email directly, view it on GitHub
<#31 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AHNDZFJYSAQ2TKIUBXPROZTXDD3DLANCNFSM4WTVSV2Q>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
if your case its showing the same error unlimited times, you can use the option "id" to prevent this.
|
Any updates @timolins ? |
I tried this method since the other method with toaster store would make the animation very funky and old toasts not disappearing immediately. This method is much nicer since it just replaces the content of the toast. However it since like the last toast would linger for a long time since the duration has been accumulating for the same id? This feels like a bug and shouldnt happen though. The correct behavior should reset the timer but not accumulating it? |
.toaster-wrapper > div { display: none !important; } .toaster-wrapper > div:nth-child(-n+3) { display: flex !important; } this works |
<Toaster ... containerClassName="toaster-wrapper" ... |
Any updates on this? This is a very basic feature that shouldn't be hard to add to the library |
It is just absurd that the issue has been opened since 2021 and there is still no new addition in the api. |
I think it's time someone create a fix for this and create one PR |
Any update on this? I've just switched to using toastr but I may have to find an alternative if this is not fixed/added. |
Please add an inbuilt method/option for this, I had to implement the whole logic my self. 🫠 |
Any updates on this? I think the number limit would be an awesome feature. Also adding info() and warning() mentioned by |
Hi. here's a simple wrapper component "use client";
import React, { useEffect } from "react";
import toast, { Toaster, useToasterStore } from "react-hot-toast";
function useMaxToasts(max: number) {
const { toasts } = useToasterStore();
useEffect(() => {
toasts
.filter((t) => t.visible) // Only consider visible toasts
.filter((_, i) => i >= max) // Is toast index over limit?
.forEach((t) => toast.dismiss(t.id)); // Dismiss – Use toast.remove(t.id) for no exit animation
}, [toasts, max]);
}
export function ToasterWithMax({
max = 10,
...props
}: React.ComponentProps<typeof Toaster> & {
max?: number;
}) {
useMaxToasts(max);
return <Toaster {...props} />;
} Usage
|
Hey! Awesome library. Something that would be useful for me is an option to set the maximum number of visible toasts, that will automatically dismiss the oldest one when the maximum is reached.
I could do that with
useToaster
and a filter but then I would have to replicate everything else<Toaster />
already does, so I would prefer not to.The text was updated successfully, but these errors were encountered: