我正在使用react-modal但我的按钮关闭模态并没有触发onClick = {this.handleCloseModal}使得模态内的按钮完全没有响应。我已经尝试了几个小时的不同事情,无法弄清楚为什么这不起作用。控制台中没有错误弹出,我无法控制日志的任何内容....请看下面的代码,我刚刚在墙上敲打着没有答案的墙。我很新的反应,但觉得这应该工作,因为没有错误。提前谢谢!
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>
)
}
}
答案 0 :(得分:1)
尝试删除li标签外的模态?我在这里只是猜测一下,也许它会触发onClick = {this.handleOpenModal},无论你点击Modal,因为它是列表项的孩子?值得一试:))