-1

I am not sure why I can't call my class method in forEach loop. Is there a way to fix this?

This works:

this.displayMaps()

But, this doesn't work:

this.state.floors.forEach(function(floor) {
  this.displayMaps(floor)
})
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
kevin
  • 309
  • 2
  • 12

1 Answers1

2

The scope of this changes inside the forEach callback function.Instead use arrow function

{
  this.state.floors.forEach((floor) => {
    this.displayMaps()
  })
}
brk
  • 48,835
  • 10
  • 56
  • 78