react-modal: Cannot register modal instance that's already open
When a modal is open, double-clicking on the background overlay results in the modal disappearing then quickly reappearing again with the following warning in the console:
react_devtools_backend.js:6 React-Modal: Cannot register modal instance that's already open
at ModalPortal (http://localhost:4500/static/js/0.chunk.js:60569:5)
at Modal (http://localhost:4500/static/js/0.chunk.js:60212:5)
Additionally, the ReactModal__Body--open class remains on the body after closing the re-opened modal.
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 2
- Comments: 38 (1 by maintainers)
Commits related to this issue
- fix: react-modal unmount bug - ref: https://github.com/reactjs/react-modal/issues/808 — committed to dezswap/dezswap-web-app by honeymaro a year ago
- fix: react-modal unmount bug - ref: https://github.com/reactjs/react-modal/issues/808 — committed to dezswap/dezswap-web-app by honeymaro a year ago
- [fixed] Prevent unintended register and unregister - Fixed [reactjs/react-modal#808](https://github.com/reactjs/react-modal/issues/808) - Checking `this.props.isOpen` instead of `this.state.isOpen` i... — committed to honeymaro/make-react-modal-great-again by honeymaro 5 months ago
I am now getting this error after upgrading to React 18. Prior to doing the upgrade, I did not see this error:
React-Modal: Cannot register modal instance that's already open. Does anybody have any idea of what might have changed?I was having this error (react-modal@3.15.1, React@18.2.0), and after a lot of frustration found out that my problem was caused by using
React.StrictModein my root element, which apparently double-invokes some lifecycle methods in development mode (not production, though). As of now, this behavior is documented in theStrictModedocs – search for “double-invoking the following functions”.I found this solution by accident, because I was investigating an effect being called twice and found this answer, which also referenced this answer which has more info about the double rendering of
StrictMode.It seems like this is harmless in my case because the only visible issue is the warning in console (modals are still displaying fine), but if anyone has a suggestion for suppressing the warning, I’d appreciate it! Removing the
StrictModetag stops this issue from happening, but I want to keep the benefits of using strict mode.I don’t
I don’t want to be a top react dev, just fix the issue please.
Why don’t you fix it, @mleister97?
Hmmmm…It’s recommended to not use modal with conditional rendering. There is a problem with createPortal when using this way (need to check if it still happening. It starts happening on version +16.3 of react). Are you all using conditional rendering?
This solved the issue for me.
I was conditionally rendering in my component, rather than letting the
isOpenprop handle the rendering.Please read this to help debugging this issue
It’s recommended to not use modal with conditional rendering. There is a problem with createPortal when using this way (need to check if it still happening. It starts happening on version +16.3 of react). Are you all using conditional rendering?
A suggestion is: don’t use conditional rendering with modals…there is no need to. There is no performance cost, there is no problem to maintain (it’s only one state).
Sorry for this guys… 😂 But please let me know if you have a different case where this happens other than with conditional rendering.
here: https://stackblitz.com/edit/nextjs-aznh2l?file=pages%2Findex.js
without
modalIsOpen &&error doesn’t occurYes, but I think it occurs only in development mode with react 18 and enabled StrictMode https://github.com/reactwg/react-18/discussions/19
Hey @diasbruno, want me to take this one? Feel free to assign it to me if so. 🙇
Issue is here: https://github.com/reactjs/react-modal/blob/master/src/components/ModalPortal.js#L136
When strict mode unmounts
isOpenis not yet initialized so it’s not de-registering the modal correctly so it leaks instances and focus handlers.This “works”
But honestly whole thing should be rewritten.
We Can’t use:
{modalIsOpen && (......)The best way to use that is creating a component where we have all the code that we need, a componet seems something like this:<Modal isOpen={modal} style={customStyles} > <ModalProducto /> </Modal>and ModalProducto.jsx is something like this:export default function ModalProducto() { return ( <div>ModalProducto</div> ) }That way works for me
No podemos usar:
{modalIsOpen && (......)Lo que se debe hacer es crear un componente con toda la lógica que necesitamos, algo parecido a lo siguiente:<Modal isOpen={modal} style={customStyles} > <ModalProducto /> </Modal>y ModalProducto.jsx es algo como lo siguiente:
export default function ModalProducto() { return ( <div>ModalProducto</div> ) }Esa es la solución.I have the same issue.
no conditional rendering, no strict mode.
react-modal
3.16.1, react18.2.0@rodriguezmarting Awesome. This is weird… Can you make a step to reproduce so we can see how are you triggering this behavior?
The bug also happens when the Modal lives next to a component that conditionally renders. Plus it only happens when using
base|afterOpen|beforeCloseclassNames.Something like this won’t work:
Facing the same issue for one of my modals after upgrading to React 18. Any fixes to this? Funnily enough, the modal starts working “after a while” (I think when I leave the app and come back). Super strange