0

Can anyone help me, I am getting this error "App.js:69 Uncaught TypeError: Cannot read properties of undefined (reading 'setState') at onInputChange (App.js:69:1)" Code Snippet

2 Answers 2

1

Few ways to go about this.

Since you're not using arrow function you need to bind your handlers

constructor() {
  ...code
  this.onInputChange = this.onInputChange.bind(this)
  this.onButtonSubmit = this.onInputChange.bind(this)
}

option 2 - switch your function to arrow function

onInputChange = (event) => {
  ...code
}

onButtonSubmit = () => {
  ...code
}

Here's a helpful post to help you understand why

https://www.freecodecamp.org/news/this-is-why-we-need-to-bind-event-handlers-in-class-components-in-react-f7ea1a6f93eb/

Sign up to request clarification or add additional context in comments.

Comments

0

You can either change onInputChange method to use arrow function or you need to bind this for the onInputChange function.

Using arrow function:

onInputChange = (event) => {
  this.setState({ input: event.target.value });
};

Using bind:

<ImageLinkForm onInputChange={this.onInputChange.bind(this)} onButtonSubmit={this.onButtonSubmit.bind(this)} />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.