Skip to content

Remounting the ToastContainer causes it to lose state #1200

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

Open
mkrause opened this issue Jan 9, 2025 · 5 comments
Open

Remounting the ToastContainer causes it to lose state #1200

mkrause opened this issue Jan 9, 2025 · 5 comments

Comments

@mkrause
Copy link

mkrause commented Jan 9, 2025

I'm trying to use react-toastify in an app that uses HTML <dialog> elements for modals. Due to the issues described here, in order to make this work with toast notifications, I need to use createPortal() to render the <ToastContainer/> inside the <dialog>. Otherwise toasts will be (1) rendered behind the modal, and (2) will not be interactive because they're in the "inert" part of the document.

I'm using createPortal() to render the <ToastContainer/> in the top-most <dialog> modal. However, if any toast messages are already open, this state is lost when the modal is opened/closed.

It seems that the state itself is already stored in a single global store (given that containerId is constant). So it should be possible for the <ToastContainer/> to get this state on re-mount so that it shows any toast messages that are already there.

@lcoronelp
Copy link

Exactly, this is the issue we are facing right now (before, we only displayed one toast at a time by flushing and dismissing everything else each time a new toast was triggered), but with multiple toasts, it doesn't retain the state.

The autoclose timer also resets, so if it was close to auto-closing, it restarts.

@Link2Twenty
Copy link

Using moveBefore to move things rather than remounting keeps the current state.

Might be a while before it's everywhere without polyfills though 😢

@mkrause
Copy link
Author

mkrause commented Apr 22, 2025

@Link2Twenty You mentioned polyfills, but moveBefore is not polyfill-able to my knowledge? Unless I missed something.

In any case I basically ended up having to write my own toast implementation because of this issue, as far as I can tell there's no way to make it work with the library as is. In previous versions we could maybe make it work by rolling a custom ToastProvider but the internal definitions are no longer exposed.

@Link2Twenty
Copy link

I've got a simplistic polyfil in here (though it in no way does everything moveBefore does), that keeps the state when moving the toast container.

https://codesandbox.io/p/sandbox/htj44g

@mkrause
Copy link
Author

mkrause commented Apr 23, 2025

@Link2Twenty Oh awesome, thanks for that. I might adopt it. Currently in my implementation I just set a flag to prevent animations from running again after the remount, but it's not as seamless as your polyfill.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants