2

I'm using React JS and Ant Design for my project.

Problem

I'm creating virtual DOM element. It has Popover In that there is Button and then clicking that showing Modal.

It showing error Cannot read property 'setState' of undefined

JS Code

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

Full Code on StackBlitz

Maria Jeysingh Anbu
  • 3,164
  • 3
  • 34
  • 55

2 Answers2

1

You need to bind the method to the proper scope:

content = (
  <div className="RecurringPopover"> 
    <button onClick={this.showModal.bind(this)}> Show Modal </button> 
  </div>
);
Gorka Hernandez
  • 3,890
  • 23
  • 29
  • @MariaJeysinghAnbu Use the `visible` and `onVisibleChange` props as detailed in the documentation. **Sample:** https://stackblitz.com/edit/react-hk6f7r?file=index.js | **Docs:** https://ant.design/components/popover/ – Gorka Hernandez Feb 23 '18 at 12:32
  • How I suppose to do when I have muliple popover in page https://stackblitz.com/edit/react-wfaivz?file=index.js – Maria Jeysingh Anbu Feb 23 '18 at 13:14
  • This is getting out of topic, but you would need to have different properties in your state object and handle them separately. – Gorka Hernandez Feb 23 '18 at 14:07
0

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
    });
}
tetar
  • 682
  • 1
  • 10
  • 25