首页 > 解决方案 > that.setState 不是函数。(expo react native)

问题描述

这是我在课堂上的功能:

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)

标签: react-nativeexpo

解决方案


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

应该

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

推荐阅读