反应 |虚拟 DOM 模式未显示

React | Virtual DOM Modal is not showing

我正在为我的项目使用 React JS and Ant Design

问题

我正在创建虚拟 DOM 元素。它有 Popover 因为有 Button 然后单击显示 Modal

显示错误Cannot read property 'setState' of undefined

JS代码

content = (
  <div className="RecurringPopover"> 
    <button onClick={this.showModal}> Show Modal </button> 
  </div>
);

StackBlitz

上的完整代码

您需要将方法绑定到正确的范围:

content = (
  <div className="RecurringPopover"> 
    <button onClick={this.showModal.bind(this)}> Show Modal </button> 
  </div>
);

添加你的构造函数

 constructor(props) {
    super(props);
    this.showModal = this.showModal.bind(this)
    this.state = {
        // hereyour state
    };
}

onClick={this.showModal.bind(this)}

要关闭或打开模式,您可以这样做

 showModal() {
    this.setState({
        modal: !this.state.modal
    });
}