0

I'm having trouble rendering multiple Snackbars, I know it's not allowed to render multiple at once, so I tried notistack,but when I put maxSnack={1} autoHideDuration={3000} and I enqueue 2 Snackbars, then the first one disappears immediately, and I would need to make it stay for those 3 seconds. Therefore I've decided to create own solution. I have an array of messages, and I want to render Snackbars with those messages like render 1st one for 3 seconds, then 2nd one etc.

I've tried to accomplish this with setTimeout() but didn't succeed. I am not sure how to approach this problem. Here's what I got so far:

  export const NotificationsInterval = (props: INotificationsProps) => {
    const [notifications, setNotifications] = React.useState<IListItem[]>([]);

    const [currentIndex, setCurrentIndex] = React.useState<number>(0);
    const [currentItem, setCurrentItem] = React.useState<IListItem>();

    React.useEffect(() => {
        const timer = setTimeout(() => {
        setCurrentItem(props.notifications[currentIndex]);
        setCurrentIndex(currentIndex => currentIndex + 1);
    }, 1000);
    return () => clearTimeout(timer);
    }, []);

  return (
    <>
    {notifications.length !== 0 && notifications.map(n => {
    {currentItem.Title}
    })}
    </>
  );

}

  • I know the code isn't working. I just wanted to show that I've already tried something. I need to know the approach to solve this, I don't need the exact code :) – Luci Castle Nov 25 '20 at 19:10
  • My suggestion is finding the right library for solving the issue. It's not a trivial piece of work to create a flawless, animated component on your own. – technophyle Nov 25 '20 at 19:13
  • "Therefore I've decided to create own solution." just do not try to do it all at once. :) First think of the API you want your solution to have, then split it into small steps like for example creating some `Queue` helper that allows to enqueue things and has `start/pause/stop` capabilities or having a `useQueue` hook that binds your helper to React or relies on `useReducer` under the hood, something that you can implement and test separately. Then you could go with representation components to actually show notifications once the logic is done. – Yury Tarabanko Nov 25 '20 at 19:22
  • Would [react-toastify](https://www.npmjs.com/package/react-toastify) help solve this problem for you? – Drew Reese Nov 25 '20 at 22:41
  • react-toastify looks great! thanks – Luci Castle Nov 26 '20 at 23:43

0 Answers0