React native that.setState不是函数。(本机)

React native that.setState不是函数。(本机),react-native,expo,React Native,Expo,这是我在课堂上的作用: loadFeed = () => { this.setState=({ refresh: true, collection: [] }); var that = this; database.ref('collection').orderByChild('date').once('value').then(function(snapshot) { const exists = (sn

这是我在课堂上的作用:

loadFeed = () => {
    this.setState=({
        refresh: true,
        collection: []
    });

    var that = this;

    database.ref('collection').orderByChild('date').once('value').then(function(snapshot) {
        const exists = (snapshot.val() !== null);
        if(exists) data = snapshot.val();
        var temp = that.state.employees;

        for(var item in data){
            var Obj = data[photo];

            database.ref('users').child(Obj.name).once('value').then(function(snapshot){
                const exists = (snapshot.val() !== null);
                if(exists) data = snapshot.val();

                temp.push({
                    id: key,
                    name: Obj.name
                });

                that.setState({
                    refresh: false,
                    loading: false
                });

            }).catch(error => console.log(error)); 
        }
    }).catch(error => console.log(error));
}
在上面的代码中,我遇到了以下错误:

that.setState不是一个函数

  (In 'that.setState({
            refresh: false,
            loading: false
          })', 'that.setState' is an instance of Object)
应该是

this.setState({
    refresh: true,
    collection: []
});

而不是使用
var=this
您应该将其绑定到您尝试使用
this
的上下文中

发件人:

bind()方法创建一个新函数,在调用该函数时,将其this关键字设置为提供的值,并在调用新函数时在任何提供的参数之前提供给定的参数序列

如果您不熟悉这一点,您应该了解JavaScript中的绑定:


是的,你明白了!我认为问题与绑定有关
this.setState({
    refresh: true,
    collection: []
});
const module = {
  x: 42,
  getX: function() {
    return this.x;
  }
}

const unboundGetX = module.getX;
console.log(unboundGetX()); // The function gets invoked at the global scope
// expected output: undefined

const boundGetX = unboundGetX.bind(module);
console.log(boundGetX());
// expected output: 42