ListView中的RefreshControl因某些未知原因而无效

时间:2016-04-07 10:04:27

标签: reactjs react-native react-native-listview react-native-scrollview

当我下拉刷新时出现错误。 enter image description here

代码是这样的 -

loadData: function(){

    var _this = this;
    this.API();

    //check if data is loaded
    setTimeout(function(){
        if(_this.isMounted()){
            if(_this.state.loaded===false){
                _this.setState({
                    isReloadRequired: true
                })
            }
        }
    }, 10000);
},
API: function(){

    var _this = this;
    this.setState({ rawData: [], isReloadRequired: false, loaded: false, isRefreshing: true });

    Parse.Cloud.run('fetchBookingListForUserCloudFunction', {
        user_id: Parse.User.current().getUsername()
    }).then(

        function(result){
            var cleanData = [];
            for(var i=0;i<result.length;i++){
                cleanData.push(result[i].toJSON());
            }

            _this.setState({
                rawData: _this.state.rawData.concat(cleanData),
                dataSource: _this.state.dataSource.cloneWithRows(cleanData),
                loaded: true,
                isReloadRequired: false,
                isRefreshing: false,
            });
        },
        function(error){
            _this.setState({ isReloadRequired: true, loaded: false, isRefreshing: false })
            console.log("[HOME API] Error: "+ JSON.stringify(error, null, 2));
        }
    );
},
render: function(){
    return(
        {this.state.loaded ? this.renderListView() : this.renderLoadingView()}
    )
},

renderListView: function(){
    if(this.state.rawData.length === 0){
        return(
            <View style={styles.container}>
                <Text>Tap to book.</Text>
            </View>
        );
    }
    return(
        <ListView 
            dataSource={this.state.dataSource}
            renderRow={this.renderReservation}
            style={styles.listView}
            refreshControl={
                <RefreshControl 
                    refreshing={this.state.isRefreshing}
                    onRefresh={this.loadData}
                />
            }
        />
    );          
},

renderLoadingView: function(){
    return(
        <View style={styles.container}>
            <LoadingView />
        </View>
    )
},

出了什么问题?我也无法在github上找到任何东西。 出了什么问题?我也无法在github上找到任何东西。

已更新

您好。感谢您在rnplay上创建示例。但是我已经将问题的原因缩小到了这个范围 - 每当我在RefreshControl内使用ListViewrender()时,它就会按预期工作。但是当我从ListView等其他函数返回RefreshControl renderlistView()时,会抛出上述错误。我甚至试过this.loadData().bind(this),但这也无济于事。

1 个答案:

答案 0 :(得分:0)

您是否忘记绑定loadData函数? onRefresh={this.loadData.bind(this)}如果没有看到loadData函数本身,我无法判断,但这可能就是原因。

<强>更新

我已经做了一个或多或少坚持上面代码的工作示例(由于我没有完整的代码,因此简化了一些内容):https://rnplay.org/apps/h-rK5g

与您的示例类似,它从服务器(这里是模拟json端点)获取数据并使用结果更新列表视图。

在设置状态之前,你可以检查你的cleanData是否已经完全加载(在for循环和toJSON()调用之后)?