AKJ
AKJ

Reputation: 819

_this3.state.method is not a function

I have a .js file as such:

My Class Component:

[...]
constructor(props, context) {
    [...]
    this.method1 = this.method1.bind(this);
}

anotherMethod() {
    [...]
    this.state.method1();
}

method1() {
    //Do something
}

I am getting this error: _this3.state.method1 is not a function. I tried following some other known solutions in here.

I have a few questions:

  1. What is the root cause?
  2. What is _this3 an where did it come from?

Thanks.

EDIT: I found the solution which is to remove the "state". Instead of this.state.method1(), it should be this.method1(). Just curious how come i do not need the "state" when i bind it above?

Upvotes: 0

Views: 709

Answers (1)

Clark
Clark

Reputation: 305

Because method1 not in state, This is component self function, Unless method1 in state like the following.

this.state = {method1:() =>{ /*Do something*/ }}

You can read React website about state and Handling Events.

Upvotes: 2

Related Questions