【问题标题】:_this3.state.method is not a function_this3.state.method 不是函数
【发布时间】: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 中遵循其他一些已知的解决方案。

我有几个问题:

  1. 根本原因是什么?
  2. _this3 是什么?它是从哪里来的?

谢谢。

编辑:我找到了删除“状态”的解决方案。而不是this.state.method1(),它应该是this.method1()。只是好奇为什么我在上面绑定它时不需要“状态”?

【问题讨论】:

  • 您似乎不知道stateReact 上下文中是什么。我建议你在React阅读更多关于开发模式的信息。

标签: javascript reactjs binding es6-class


【解决方案1】:

因为method1不在state中,这是组件自身的功能,除非method1state中像下面这样。

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

您可以阅读React 网站了解stateHandling Events

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-13
    • 1970-01-01
    • 2018-01-02
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    • 2017-05-06
    • 2019-12-20
    相关资源
    最近更新 更多