简体   繁体   English

反应 | 虚拟 DOM 模式未显示

[英]React | Virtual DOM Modal is not showing

I'm using React JS and Ant Design for my project.我在我的项目中使用React JSAnt Design

Problem问题

I'm creating virtual DOM element.我正在创建虚拟 DOM 元素。 It has Popover In that there is Button and then clicking that showing Modal .它有Popover In 有Button然后单击显示Modal

It showing error Cannot read property 'setState' of undefined它显示错误Cannot read property 'setState' of undefined

JS Code JS代码

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

Full Code on StackBlitz StackBlitz 上的完整代码

You need to bind the method to the proper scope:您需要将方法绑定到正确的范围:

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

add on your constructor添加您的构造函数

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

or或者

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

To close or open the modal you can do this要关闭或打开模态,你可以这样做

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

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

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