question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

limit total number of toasts

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:6
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

31reactions
timolinscommented, Mar 20, 2021

You can achieve by using useToasterStore() & useEffect(). We can read the state of all toasts, and dismiss them if our limit is reached.

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.

5reactions
timolinscommented, Dec 13, 2021

According to the demand, it’s time for a proper API. Will look into this for the next release.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Toast / How To / Restrict the maximum number of Toasts to show
Restrict the maximum number of Toasts to show in React Toast component. You can restrict the maximum toast count by event callback function....
Read more >
Limit the number of toast displayed | React-Toastify
Limit the number of toast displayed. Notifications are useful to get the attention of the user. But displaying too many of them can...
Read more >
Is there a limit to the amount of displayable Toasts?
Yes, Toasts are queued and there is a limit of 50 Toasts, you can see the check for it in NotificationManagerService class if...
Read more >
[UWP]Length limits to number of lines and characters ... - MSDN
Toasts can only have up to 3 text elements. Sorry for the confusion in the article, we'll work on updating it to make...
Read more >
Rate limiting - - Developer guide - Toast APIs
The global rate limit limits the total number of requests that an API client can make across all APIs collectively. For example, if...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found