0

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?

AKJ
  • 749
  • 6
  • 29
  • It seems you are not aware of what `state` is in `React` context. I recommend you to read more about development pattern in `React`. – AkshayM Nov 13 '18 at 05:19

1 Answers1

2

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.

Clark
  • 305
  • 1
  • 8