reactjs - 单击覆盖层时 react 模式未关闭

标签 reactjs modal-dialog react-modal

我正在使用react-modal

文档提到,默认情况下,当您单击叠加层时,模式应关闭。即使我将 shouldCloseOnOverlayClick 属性设置为 true,这仍然不起作用。

我不知道有什么可能阻止该事件发生。

如果这与任何内容相关/指示任何内容(并且我还没有弄清楚为什么会显示此内容),我在 Chrome 开发人员工具中注意到我的模式的覆盖层和内容节点都有一个未定义的类。我使用的所有 CSS 类都已定义并按其应有的方式工作。

这是相关的 JSX 和 CSS,如果需要更多上下文,请告诉我。

JSX:

return (
    <div className="Modal">
        <Modal
            className={{
                base: 'Modal-content' + ' Modal-InputError-videoURL'
            }}
            overlayClassName={{
                base: 'Modal-overlay'
            }}
            isOpen={true}
            contentLabel="Modal"
        >
            {props.message}
            <br />
            <br />
            <br />
            <button
                className="Modal-button"
                onClick={events.handleCloseModal}
            >
                Close
            </button>
        </Modal>
    </div>
)

CSS 类:

.Modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.35);
    z-index: 9998;
}

.Modal-content {
    display: relative;
    position: absolute;
    top: 50%;
    left: 50%;
    border: 1px solid #ccc;
    background: rgb(38,38,38);
    border: 1.5px solid rgb(45,45,45);
    overflow: auto;
    border-radius: 1px;
    outline: none;
    z-index: 9999;
    width: 400px;
    margin-left: -150px;
    margin-right: -150px;
    padding: 24px;
    line-height: 16px;
}

.Modal-InputError-videoURL {
    height: 134px;
    margin-bottom: -67px;
    margin-top: -67px;
}

.Modal-button {
    display: inline-block;
    padding: 4px;
    margin: 0;
}

最佳答案

我通过使用onRequestClose(根据文档)解决了这个问题。看来 react-modal 不会在其本地状态中存储 isOpen ,因此我提供了一个对 onRequestClose 的回调来更新 state 在父组件中,随后作为 prop 传递给模态。

http://reactcommunity.org/react-modal/examples/on_request_close.html

关于reactjs - 单击覆盖层时 react 模式未关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45258227/

相关文章:

datepicker - 角度 Material md-datepicker 无法在 ionic 模式下工作

react-native - 导航到另一个屏幕时关闭模式

javascript - React 将 prop 作为函数传递,然后调用它 error

twitter-bootstrap - 具有 2 个模式的 Bootstrap,屏幕卡住

javascript - React + .jsx + Spread 运算符——添加样式?

javascript - 将函数传递给 React Component 然后调用它会导致 "Uncaught TypeError: x is not a function"

reactjs - 将 react 表行数据传递给 react 模式

reactjs - .React Material Botton 轮廓有黑色边框颜色 onclick 。但是我没有设置。如何删除它?

reactjs - 在 React 中,如何在所有组件中拥有一个自定义钩子(Hook)的单个实例?