Set State - Random Quote Machine

I’m still having some trouble with the Random Quote Machine. Right now:
-Inside componentDidMount(), I have the fetch request to the external JSON file.
-It gets all the quotes data and saves it to a variable called myJson.
-I know it’s working because I can console.log individual quotes and authors from myJson.

The problem is:
-On line 23 when I try to use setState to give quotesData the value of myJson, it breaks the code.
-If I delete the setState, the 3 consoles that don’t rely on state will be logged.

I don’t really understand why setState is not working? Should componentDidMount() be calling a separate function to fetch the data and change the state?

The keyword function binds this to itself. Replace all your function with the arrow function.

If you still want to use function for any reason, then you need to store the class this into a variable, example:

componentDidMount() {
  const classThis = this;

...

classThis.setState ({

Like I said, the arrow function is way better than this mess :+1:

1 Like

Oh my gawd, that made it work! It’s such a simple thing but I’m so happy that it’s actually doing something now. Thank you so much! :grin: