简体   繁体   English

需要时可以成功打开模态但无法关闭它

[英]Can successfully open modal when I need to but cannot close it

I am trying to use React boostrap modal, I can successfully open it when i need to but I cannot close it.我正在尝试使用 React boostrap 模式,我可以在需要时成功打开它,但无法关闭它。 I have no idea what i am doing wrong.我不知道我做错了什么。

my markup我的标记

```                <Modal show={this.props.show} >
                    <Modal.Header>
                        <Modal.Title>Enter Log</Modal.Title>
                    </Modal.Header>
                    <Modal.Body>
                        Hello i am Modal!!!!!
                    </Modal.Body>
                    <Modal.Footer>
                    <Button onClick={() => {closeModal()}}  variant="primary"> Close </Button>
                    </Modal.Footer>```

state:状态:

constructor(props) {
    super(props)   
    this.state = {
      closeModal: false
    }
}

Handler function:处理函数:

const closeModal = () => {
    this.setState({closeModal: true}); 
}

You can do this way,你可以这样做,

const closeModal = () => {
    this.setState({ show: false});
}

<Modal show={this.props.show} onHide={this.props.closeModal} >
    <Modal.Header closeButton>
        <Modal.Title>Enter Log</Modal.Title>
    </Modal.Header>
    <Modal.Body>
       <p> Hello i am Modal!!!!! </p>
    </Modal.Body>
    <Modal.Footer>
        <Button onClick={this.props.closeModal} variant="primary"> Close </Button>
    </Modal.Footer>
</Modal>

refrence https://react-bootstrap.github.io/components/modal/参考https://react-bootstrap.github.io/components/modal/

You need to use the same condition to check if render the modal or not您需要使用相同的条件来检查是否渲染模态

You are using props to check if your modal should be rendered and when closing it, you are setting state to false.您正在使用props来检查是否应呈现您的模态,并在关闭它时将state设置为 false。

If you want to use props you will need to update your props, using redux and dispatching an action or whatever如果你想使用props你需要更新你的 props,使用 redux 和 dispatch 一个 action 或其他什么

According to yor handleClose function, you will need to check state instead of props in the condition根据 yor handleClose 函数,您将需要检查state而不是条件中的props

const closeModal = () => { this.setState({closeModal: true}); }

<Modal show={this.props.show} >
                <Modal.Header>
                    <Modal.Title>Enter Log</Modal.Title>
                </Modal.Header>
                <Modal.Body>
                    Hello i am Modal!!!!!
                </Modal.Body>
                <Modal.Footer>
                <Button onClick={() => this.closeModal()} variant="primary"> Close </Button>
                </Modal.Footer>

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

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