简体   繁体   English

React.js 对话框组件消失但仍保留在 DOM 中

[英]React.js dialog component disappears yet remains in DOM

I'm using a Dialog component from Material-UI which, for the most part, is working correctly.我正在使用 Material-UI 的 Dialog 组件,它在大多数情况下工作正常。 However, if I click away from the Dialog, the Dialog will disappear (as expected) but sometimes, it remains in the DOM with its opacity set to 0, and I can't click anything else since the Dialog is in the way.但是,如果我在对话框之外单击,对话框将消失(如预期的那样),但有时,它仍保留在 DOM 中,其不透明度设置为 0,并且我无法单击其他任何内容,因为对话框挡住了路。 This is a small sample of my code:这是我的代码的一个小示例:

  const [openDialog, setOpenDialog] = React.useState(false);

  React.useEffect(() => {
    // Get data for ReactTable
  }, []);

  return(
    <div>
      // Other components
      <Button color="white" onClick={() => setOpenDialog(true)}>
         Open Dialog
      </Button>
      // Other components
      <Dialog open={openDialog} maxWidth="md" onClose={() => setOpenDialog(false)}>
        // ReactTable and close button
      </Dialog>
    </div>
  )

This bug doesn't always occur which makes it tricky to debug.这个错误并不总是发生,这使得调试变得很棘手。 I've only been using React for about a month, but I'm wondering if it's a state problem, or maybe some sort of race condition.我只使用 React 大约一个月,但我想知道这是否是 state 问题,或者可能是某种竞争条件。 Any suggestions?有什么建议么?

Edit: This also occurs when a DropzoneDialog appears, to upload a file.编辑:当出现 DropzoneDialog 以上传文件时,也会发生这种情况。

This also works:这也有效:

  <Dialog className={openDialog ? "" : classes.displayNone} open={openDialog} maxWidth="md" onClose={() => setOpenDialog(false)}>
   // ReactTable and close button
  </Dialog>

where in the styles file you have:在 styles 文件中,您有:

  displayNone: {
    display: "none"
  }

In case anyone else has this same issue, I found the answer:如果其他人有同样的问题,我找到了答案:

Elsewhere in the app, useEffect() was stuck in a loop and running extremely frequently which slowed the app down, causing this problem.在应用程序的其他地方, useEffect()卡在一个循环中并且非常频繁地运行,这会减慢应用程序的速度,从而导致此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM