Skip to content
Advertisement

React Class Component setState undefined

I am attempting to update a variable from “” to a string of text with an onClick method in React. I’ve approached this several different ways following examples on the web and Youtube but all are resulting in various error messages.

This is the latest attempt and I’m receiving an error message that it cannot set properties of undefined setting set state & it’ pointing to the line that reads “super(props).

Can you please let me know what is wrong with this approach?

JavaScript

Advertisement

Answer

  1. You do not need the global variable response.
  2. No need for the componentDidMount either, you already set the initial state in the constructor.
  3. setState is a function, so you need to call it and not assign something to it
  4. use an arrow function for the autoResponse, if you intend to pass it as a prop to other components, so that it retains the correct this.

JavaScript
Advertisement