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)
})
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)
})
The scope of this
changes inside the forEach
callback
function.Instead use arrow function
{
this.state.floors.forEach((floor) => {
this.displayMaps()
})
}