0

I am working on react js and import confirm modal from library but it does not allow me to user "this" within onOk() method

import {Modal} from "antd";
const confirm = Modal.confirm;
class Chat extends Component {
constructor(props){
    this.state =  {
    }
    this.deleteajax=this.deleteajax.bind(this);
    this.deletemethod=this.deletemethod.bind(this);
}
deleteajax(){
   // code for ajax
}
deletemethod(){
   confirm({
     title: 'Are you sure you want to delete this measureemnt?',
     okText: 'Yes',
     okType: 'danger',
     cancelText: 'No',
     onOk() {
          this.deleteajax(id);
     }
 }
  render(){
  //some design for delete button
   }
 }
Aspl Test
  • 133
  • 1
  • 10
  • 1
    You need to use an arrow function. – SLaks May 02 '19 at 13:50
  • Possible duplicate of [How to access the correct \`this\` inside a callback?](https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback) – Dan O May 02 '19 at 14:00
  • there are many, many, MANY questions on Stack Overflow regarding `this` being undefined inside of callback functions. Which of those questions have you researched, which solutions have you attempted, and why were they not successful for you? – Dan O May 02 '19 at 14:00

1 Answers1

1

try this:

confirm({
     title: 'Are you sure you want to delete this measureemnt?',
     okText: 'Yes',
     okType: 'danger',
     cancelText: 'No',
     onOk: (id) => this.deleteajax(id)
     })
Roy.B
  • 2,076
  • 14
  • 23