(Solved) React.js can't set state

(edit) Scratch everything, I just had to remember to add .bind(this) after the promise call. Found it literally a couple of minutes after posting this. I guess it can help anyone else who runs into it.

Hi, quite new to ReactJS, working on the second project (Camper Leaderboard). I’m using Axios to handle calls to FCC’s leader API, all is working ok, but I can’t set the component’s state to reflect the returned data.

Error message is: Uncaught (in promise) TypeError: Cannot read property ‘setState’ of undefined

So I can see that React doesn’t know what the “this” is in this.setState. Here’s the pen. (At the moment I’m just working on the initial page load, I’ll worry about toggling later)

How do I tell the promise function that “this” is referring to the component state?

1 Like