2

我正在为我的项目使用Ant DesignReact js

问题

我在 Popover 中有一个按钮,并且该按钮具有单击事件。问题是按钮单击事件未触发。

JS代码

const content = (
  <div className="RecurringPopover"> 
    <button onClick={this.handleClick}> Popover Button </button> 
  </div>
);

stackblitz场景的完整代码

4

2 回答 2

2

您已content在类外部定义,然后将this.handleClick其作为单击处理程序提供给它。然而课外,this并不指向class. 您应该content在内部定义class并使用this.content来访问它。

handleClick() {
      alert('test');
    }
// put it inside class
 content = (
  <div className="RecurringPopover"> 
    <button onClick={this.handleClick}> Popover Button </button> 
  </div>
);
  render() {
    return (
      <div>
        <Row>
          <Col span={12}>      
          // Use this.content instead of just content
          <Popover content={this.content} title="Title">
            <span type="primary">Hover me (Popover Button)</span>
于 2018-02-22T19:01:43.683 回答
0

只需将' content'带入类内部,并通过' this.content'将其传递给组件,它就可以工作。

于 2018-02-22T19:11:03.210 回答