简体   繁体   English

即使模态尚未打开,mdbreact 模态也会将“模态打开”类应用于正文标记

[英]mdbreact modal applies a class 'modal-open' to body tag even if the modal is not open yet

I am using mdbreact modal in my react application but facing some issue.我在我的反应应用程序中使用 mdbreact 模态,但面临一些问题。 I try to include the modal component in my page, but the modal is not open yet, inspite of that it is applying a class 'modal-open' to the body tag which stops body from scrolling.我尝试将模态组件包含在我的页面中,但模态组件尚未打开,尽管它正在将类“模态打开”应用于阻止 body 滚动的 body 标记。

My code is as follows:我的代码如下:

import React, { Component } from 'react';
import { Modal, ModalBody, ModalHeader, ModalFooter } from 'mdbreact';

class GroupContainer extends Component {
    constructor() {
        super();
        this.state = {
            modal: false,
        }
    }

    renderModal = () => {
        this.setState({
            modal: true,
        })
    }

    closeModal = () => {
        this.setstate({
            modal: false,
        })
    }

    render() { 
        return (
        <div>
             <Modal isOpen={this.state.modal} toggle={() => this.renderModal()} fullHeight position="bottom">
              test
            </Modal>

        </div>
        )
    }
};


export default GroupContainer;

The state 'modal' is false still the modal-open class is getting applied.状态 'modal' 为 false 仍然应用 modal-open 类。 It must get applied only when the state is true.只有在状态为真时才必须应用它。 I will be making the state as true on click of an external button in order to show this modal.我将在单击外部按钮时将状态设为 true 以显示此模式。 Any clue?有什么线索吗?

Each of the imports are MDB(name) ex MDBModal, MDBModalBody.每个导入都是 MDB(name) ex MDBModal, MDBModalBody。 Also for toggling you don't want two functions because your code will get confused.同样对于切换,您不想要两个函数,因为您的代码会变得混乱。 Example of a modal toggle模态切换示例

 toggle = () => { this.setState({ modal: !this.state.modal }) } //for multiple modals //modal1 //modal2 //modal3 toggle = nr => () => { let modalNumber = 'modal' + nr; this.setState({ [modalNumber]: !this.state[modalNumber] }) }

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

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