Reputation: 819
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:
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
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