【发布时间】:2018-03-28 04:54:03
【问题描述】:
所以我正在制作一个简单的 React 应用程序,它需要在单击某物时显示一个模式。我使用 react-modal 来实现这一点,并且模式正在显示,但我无法再次关闭它。代码如下:
import React from 'react';
import ReactDOM from 'react-dom';
import Paper from "./Paper";
ReactDOM.render(
<Paper title={"Title"} notes={"Notes"}/>,
document.getElementById('root')
);
以及论文定义:
import React, {Component} from 'react';
import Modal from 'react-modal';
class Paper extends Component {
constructor(props) {
super(props);
this.state = {modalIsOpen: false};
this.showModal = this.showModal.bind(this);
this.closeModal = this.closeModal.bind(this);
}
showModal() {
this.setState({modalIsOpen: true});
}
closeModal() {
this.setState({modalIsOpen: false});
}
render() {
return (
<div onClick={this.showModal}>
{this.props.title}
<Modal isOpen={this.state.modalIsOpen}>
<button onClick={this.closeModal}>close</button>
{this.props.notes}
</Modal>
</div>
);
}
}
如果我在开发人员工具中检查状态,状态就不会更新,我不知道为什么。谁能帮帮我?
【问题讨论】:
-
我看不出代码有什么问题,你有任何控制台错误吗?或许可以在渲染顶部添加一个
console.log以查看this.state.modalIsOpen的值是什么。
标签: reactjs modal-dialog state