Skip to content Skip to sidebar Skip to footer

Typeerror: Cannont Read Property `state` Of Undefined

Getting the error : TypeError: Cannot read property 'state' of undefined I face this error when i am going to get my input state value by event onSubmit() . onFormSubmit(event){

Solution 1:

You dont have context (this is not bound to your function).

You can fix that using one of the following:

First, bind this in constructor

constructor(props) {
  super(props);
  this.onFormSubmit = this.onFormSubmit.bind(this);
}

Second, use arrow functions

onFormSubmit = (event) => {
  ...
}

Third, use autobind-decorator or the like

@boundMethodonFormSubmit(event) {
  ...
}

Solution 2:

Use arrow function instead so:

Change

onFormSubmit(event){
    event.preventDefault();
    console.log(this.state.term);
}

To

onFormSubmit= (event)=>{
    event.preventDefault();
    console.log(this.state.term);
}

Post a Comment for "Typeerror: Cannont Read Property `state` Of Undefined"