0

I have one function which sets class variable to Konva.Stage and I want to add event listener to this variable in same function. But unfortunately I cannot access other class functions and variables in this event listener. Basically, I cannot access to "this". How can I solve this problem?

Here you can see my example

  setStage = () => {
    this.stage = new Konva.Stage({
      container: 'imageCanvas',
      width: this.plt.width(),
      height: this.plt.height(),
      listening: true
    });


    this.stage.on('tap', function(evt) {

        console.log("TAP TRIGGERED");
        // set active shape
        var shape = evt.target;
        this.activeShape = this.activeShape && this.activeShape.getName() === shape.getName() ? null : shape;
        // <------ CANNOT ACCESS this.activeShape

        this.baseLayer.draw(); //<------ CANNOT ACCESS this.baseLayer
    };

  }
she hates me
  • 1,212
  • 5
  • 25
  • 44
  • 1
    When you refer to this inside some event it refers to window object, you can solve that by defining this before that event for example let _this = this; then inside your function use _this.baseLayer.draw(), or simply this.stage.on('tap', () => this.baseLayer.draw()); – mocni_T Jun 17 '19 at 13:07
  • @mocni_T you're the life saver, thank you very much :) – she hates me Jun 17 '19 at 13:24

0 Answers0