-3

i want to change state when fetching data is success n the setState cannot to use because get error this.setState is not a function

enter image description here

anwar tan
  • 11
  • 3
  • https://stackoverflow.com/questions/31045716/react-this-setstate-is-not-a-function is the the problem you are getting ? – strek Dec 16 '19 at 03:30

1 Answers1

0

This is a Scope problem, replace your function callback by an arrow function like this:

apiServer.getBalance(myData.token, myData.id, response => {
  if (response.success) {
    console.log("Balance success");
    balance = response.success.balance;
    this.setState({ balance: response.success.balance });
  } else {
    //login failed
    console.log("failed");
  }
});

Happy coding.

jbergeron
  • 515
  • 3
  • 8