【问题标题】:React | Virtual DOM Modal is not showing反应 |虚拟 DOM 模态未显示
【发布时间】:2018-08-03 11:49:03
【问题描述】:

我的项目使用React JSAnt 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上的完整代码

【问题讨论】:

  • @Striped。点击弹出框内的按钮。

标签: javascript reactjs modal-dialog virtual-dom


【解决方案1】:

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

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

【讨论】:

【解决方案2】:

添加你的构造函数

 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
    });
}

【讨论】:

  • Modal 打开时我想如何隐藏弹出框。
  • 您要关闭模态框吗?
猜你喜欢
  • 2018-08-02
  • 1970-01-01
  • 2022-09-23
  • 2013-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-03
  • 1970-01-01
相关资源
最近更新 更多