React app refuses to use this.setState

I’m currently working on the random quote machine React project, and Im very close to having it done however the app doesn’t want to allow me to use this.setState within a function. It continues to give me the error “Uncaught TypeError: Cannot read property ‘setState’ of undefined” however I dont understand how that is even possible as “this” worked as I expect it to elsewhere. Basically everything besides my handleClicks() function works - I currently have it simplified to have 2 static counters for personal proof of concept and am having trouble resolving the setState of undefined error.

CodePen: https://codepen.io/AugustTGuenther/pen/vYLWVxb

Hello there,

Please provide a link to your code/project. If you do not have it hosted anywhere, pasting it to CodePen would be quite easy.

Sorry, I meant to have it in the initial post

You have not bound this to the method handleClicks…do this, and it should work.

Here is a relevant lesson: https://www.freecodecamp.org/learn/front-end-libraries/react/bind-this-to-a-class-method

Hope this helps

I just did that - and now the entire page wont render.

I suggest you go over the linked lesson, above. You have not done it quite right.

1 Like

Ahhhhh, I was thinking it came after the constructor rather than inside it. Thank you so much for your time and help!

1 Like