TypeError: Cannot read property ‘setState’ of undefined

TypeError: Cannot read property ‘setState’ of undefined

Image for post

Whenever your ?this? is undefined you?re most likely not passing an arrow function to your event.

onClick={this.play}

you?ll lose your ?this? inside the function.

To preserve your ?this? use an arrow function that invokes the function play():

onClick={() => this.play()}

You should no longer have this error 🙂

Happy coding!

11

No Responses

Write a response