error solved

Fix – cannot read property setState of undefined


If you get cannot read property setState of undefined when writing a code in React, this article will fix your problem.

The issue occurs because the this keyword is not pointing out the right scope of the component. You can fix this by binding your function:

constructor(props) {
super(props);
  state = {};
  
  this.myFunction = this.myFunction.bind(this);
}

myFunction() {
  // your function here
}

Or in the latest (and simple) version, you can use arrow function to avoid binding things. Here is the example:

constructor(props) {
super(props);
  state = {};
}

myFunction = () => {
  // your function here
}

Share on social media

//