简体   繁体   English

反应模式关闭按钮不起作用

[英]React-Modal Close button not working

I am using react-modal but my button to close the modal is not firing the onClick={this.handleCloseModal} making the button inside the modal completely unresponsive. 我正在使用react-modal,但是我关闭模式的按钮没有触发onClick = {this.handleCloseModal},使得模式内部的按钮完全没有响应。 I have been trying different things for hours and cannot figure out why this is not working. 数小时以来,我一直在尝试不同的方法,无法弄清为什么它不起作用。 No errors pop up in console and I cannot console log anything.... Please see code below, I have just been beating my head against a wall with no answers. 控制台中不会弹出任何错误,并且我也无法控制台记录任何内容。...请参阅下面的代码,我只是在头顶上打着头,没有任何答案。 I am fairly new to react but feel like this should be working as there are no errors. 我现在还很陌生,但是由于没有错误,因此应该可以正常工作。 Thank you in advance! 先感谢您!

  class Project extends React.Component {
    constructor () {
        super();
            this.state = {
              showModal: false
        };


    this.handleOpenModal = this.handleOpenModal.bind(this);
    this.handleCloseModal = this.handleCloseModal.bind(this);
}

handleOpenModal () {
    this.setState({ showModal: true });
}

handleCloseModal () {
    this.setState({ showModal: false });
}

componentWillMount() {
    ReactModal.setAppElement('body');
}

    render() {
        const details = this.props.details;

        return (
            <li className="Project" onClick={this.handleOpenModal}>
                <img className="Project-image" src={'projects/' + details.image} alt={details.name}/>
                <div className="Project-overlay">
                    <p>{details.name}</p>
                </div>
                <div >
                    <ReactModal 
                    isOpen={this.state.showModal} 
                    contentLabel="This is my Mods" 
                    shouldCloseOnOverlayClick={true}
                    onRequestClose={this.handleCloseModal}
                    >
                    <div className="modal-header">
                        <h3>{details.name}</h3>
                    </div>
                    <div className="modal-body">
                    </div>
                    <div className="modal-footer">
                        <button onClick={this.handleCloseModal}>Close Modal</button>
                    </div>
                    </ReactModal>

                </div>
                <div className="Project-tag">
                    <p>{details.tag}</p>
                </div>
            </li>
        )
    }
}

Try removing the modal outside of the li tag? 尝试删除li标签之外的模式吗? I'm just taking a wild guess here, perhaps it's triggering onClick={this.handleOpenModal} wherever you click on the Modal since it's child of the list item?? 我在这里只是一个大胆的猜测,也许它会触发onClick = {this.handleOpenModal},无论您在何处单击Modal,因为它是列表项的子项? Worth a try :) 值得一试 :)

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

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