0

I've class in js - how I can tweak body in nested function in line #4. Im getting warning from IDE:

Potentially invalid reference access to a class field via 'this.' of a nested function

is there any solution to write this properly? Or I can use this inside nested function?

fetchStickyAppointment = flow(function* () {
  fetch(/api/) 
    .then(response => response.json()) 
    .then(data => { 
      if (+data?.id > 0) { 
        this.stickyAppointment = this.currentAppointment = data; 
      } else { 
        this.stickyAppointment = data; 
        this.currentAppointment = this.findCurrentAppointment(); 
      }
    }); 
})
Rafath
  • 190
  • 1
  • 2
  • 11

1 Answers1

0

You could assign this to a variable outside the function scope and then use that.

let self = this;
fetchStickyAppointment = flow(function* () {
  fetch(/api/) 
    .then(response => response.json()) 
    .then(data => { 
      if (+data?.id > 0) { 
        self.stickyAppointment = self.currentAppointment = data; 
      } else { 
        self.stickyAppointment = data; 
        self.currentAppointment = self.findCurrentAppointment(); 
      }
    }); 
})
Jason Landbridge
  • 968
  • 12
  • 17