React-bootstrap duplicates a <Modal/> on dom.
I noticed a strange behaviour . When i render a Modal component, it is duplicated.
I noticed i'm not the only one who got this problem: https://stackoverflow.com/questions/44761650/react-bootstrap-duplicated-modal-components
Any tips how to fix the issue?
Thank you, Andrea.
This is most likely a bug in your code, where you're mounting your component that renders the modal more than once. If you have a repro, though, we'd be glad to look at it.
I'm not sure that is a bug in our project code, I realize this happens when you declare a Modal inside a wrapper, for example:
const ChildrenWrapper = ({children}) => {
return <div>{children}</div>
}
And the modal is a child in the wrapper:
const ModalWrapped= ({}) => {
return <ChildrenWrapper>
<Modal show={true}>some content</Modal>
</ChildrenWrapper>
}
Than the App :
const App = () => {
return <ModalWrapped/>
}
The result is that the instance of the Modal is rendered 2 times in the virtual dom.
I'm very curious to discuss about this approach :) Thanks a lot for your answer.
IS there actually two Modals or do you only see 2 in the React Devtools? The latter case is probably due to Modals using a portal to render the DOM components outside the body of the document, and so you see two things called "Modal" but really it's two different parts of the Modal with the same name.
It's actually 2 modals, this is the final virtual dom:
<body>
<div id="root"></div> // app root
<script src="/main.js"></script> // webpack bundle
<div>...</div> // first modal rendered
<div>...</div> // second time same modal rendered
</body>
Can be possible that <Modal/> component is rendered first time when is founded in the return statement of <ModalWrapped/> , than the second time once the <ChildrenWrapper/> renders its own children?
I hope I answered your question. :)
I am having this issue as well, and my modal is only wrapped by the container component. I have a strong feeling that it is not because of my code, but I can't be sure. Hard bug to trace.
I solved the issue moving all modals to a top level, controlling them with a state manager instead (Redux or MobX).
I am using redux, I can't move the modal anywhere higher, though.
Hi, I solved this by wrapping the modal html inside a div.
<div className="some-modal-container">
<div className="modal ..."> ........ ...... ...... </div>
</div>
Hope this help
This is definitely still an issue.
Before showing modal:

After showing modal:

Please re-open.
@RobertSigma can you post a repro on CodeSandbox?
Had the same issue. I solved it by wrapping the modal in a View at the bottom of my code. (after the last view) Just make sure there isn't another View below it
I had the same or similar issue, where my modal would be rendered twice https://github.com/mui/material-ui/issues/24641#issuecomment-1729167309
Still facing a similar issue. My modals are being rendered twice in the DOM.
Please help.
Still facing a similar issue. My modals are being rendered twice in the DOM. Please help.
have you looked into the webpack configuration? https://github.com/mui/material-ui/issues/24641#issuecomment-1729167309
Still facing a similar issue. My modals are being rendered twice in the DOM. Please help.
have you looked into the webpack configuration? mui/material-ui#24641 (comment)
Well, I'm using react, not webpack. The modal mounts once on some occasions but on others it mounts twice as seen above.
