react-bootstrap icon indicating copy to clipboard operation
react-bootstrap copied to clipboard

React-bootstrap duplicates a <Modal/> on dom.

Open andydl82 opened this issue 9 years ago • 15 comments

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.

andydl82 avatar Jul 24 '17 13:07 andydl82

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.

taion avatar Jul 24 '17 17:07 taion

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.

andydl82 avatar Jul 25 '17 08:07 andydl82

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.

jquense avatar Jul 25 '17 14:07 jquense

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. :)

andydl82 avatar Jul 25 '17 14:07 andydl82

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.

flygis avatar Oct 20 '17 09:10 flygis

I solved the issue moving all modals to a top level, controlling them with a state manager instead (Redux or MobX).

andydl82 avatar Oct 20 '17 09:10 andydl82

I am using redux, I can't move the modal anywhere higher, though.

flygis avatar Oct 20 '17 10:10 flygis

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

suhailaleryani avatar Dec 06 '18 22:12 suhailaleryani

This is definitely still an issue.

Before showing modal: Before showing modal

After showing modal: Showing modal

Please re-open.

RobertSigma avatar Jun 25 '20 08:06 RobertSigma

@RobertSigma can you post a repro on CodeSandbox?

kyletsang avatar Jun 25 '20 16:06 kyletsang

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

Ghadahb avatar Jan 23 '22 21:01 Ghadahb

I had the same or similar issue, where my modal would be rendered twice https://github.com/mui/material-ui/issues/24641#issuecomment-1729167309

pinguluk avatar Sep 21 '23 09:09 pinguluk

Still facing a similar issue. My modals are being rendered twice in the DOM. Please help. Screenshot 2023-11-06 at 07 37 11

kalekachali avatar Nov 06 '23 04:11 kalekachali

Still facing a similar issue. My modals are being rendered twice in the DOM. Please help. Screenshot 2023-11-06 at 07 37 11

have you looked into the webpack configuration? https://github.com/mui/material-ui/issues/24641#issuecomment-1729167309

pinguluk avatar Nov 06 '23 14:11 pinguluk

Still facing a similar issue. My modals are being rendered twice in the DOM. Please help. Screenshot 2023-11-06 at 07 37 11

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.

kalekachali avatar Nov 07 '23 13:11 kalekachali