【发布时间】:2019-04-15 21:14:44
【问题描述】:
我有一个这样的 .js 文件:
我的班级组件:
[...]
constructor(props, context) {
[...]
this.method1 = this.method1.bind(this);
}
anotherMethod() {
[...]
this.state.method1();
}
method1() {
//Do something
}
我收到此错误:_this3.state.method1 不是函数。我尝试在here 中遵循其他一些已知的解决方案。
我有几个问题:
- 根本原因是什么?
- _this3 是什么?它是从哪里来的?
谢谢。
编辑:我找到了删除“状态”的解决方案。而不是this.state.method1(),它应该是this.method1()。只是好奇为什么我在上面绑定它时不需要“状态”?
【问题讨论】:
-
您似乎不知道
state在React上下文中是什么。我建议你在React阅读更多关于开发模式的信息。
标签: javascript reactjs binding es6-class