首页 > 解决方案 > 遇到this.state不是函数错误

问题描述

当我尝试运行我的代码时,我得到了error this.setState is not a functiondeos,任何人都知道我该如何纠正这个问题?谢谢!

spiele.listUsers(params, function(err, data) {
        if (err) console.log(err, err.stack);
        else     console.log(data)
        const ball = data;
        this.setState({
          ball
        }).bind(this)         
      });

标签: javascriptreactjs

解决方案


.bind在错误的地方使用

spiele.listUsers(params, function(err, data) {
        if (err) console.log(err, err.stack);
        else     console.log(data)
        const ball = data;
        this.setState({
          ball
        }).bind(this)   // wrong      
      });

.bind应该在function(err, data)不在this.setState

spiele.listUsers(params, function(err, data) {
        if (err) console.log(err, err.stack);
        else     console.log(data)
        const ball = data;
        this.setState({
          ball
        })       
      }.bind(this)); // correct

推荐阅读