如何停止不断执行该功能。反应本机

时间:2019-01-18 15:43:07

标签: react-native expo

因此,我有一个应用程序在加载时调用我的RAPI nodejs服务器,我将每个调用限制为5个,例如有100张图片,它将得到100到96,然后在下一次调用95到90,依此类推。现在,我在服务器和客户端上都使用了计数器,以确保获得正确的信息。

在客户端,应用程序启动后会得到前5个,然后立即调用下一个方法,基本上会运行所有100张图片,直到用户有机会滚动。

所以我很困惑,因为有时我的代码可以工作,它只调用5,直到我滚动其他次为止,它都调用了全部100张图片。 (请尝试A。)

我做了一些挖掘,找到了另一个解决方案,但现在不调用该方法。疾病同时显示(这是TRY B。)

我已经研究了ScrollView,但它不能满足我的需要,因此我需要一个平面列表。

尝试B。

render() {
    return (
        <SafeAreaView style={styles.gridView}>
            <FlatList
                data={formatData(data, numColumns)}
                renderItem={this.renderItem}
                numColumns={numColumns}
                keyExtractor={(item, index) => index.toString()}

                refreshing={this.state.getUpdatedData}
                onRefresh={this._onRefresh}
                onEndReached={this._onEndReached}
                onEndReachedThreshold={0.2}
                onScrollBeginDrag={() => {
                  console.log('onScrollBeginDrag');
                  this.canAction = true;
                }}
                onScrollEndDrag={() => {
                  console.log('onScrollEndDrag');
                  this.canAction = false;
                }}
                onMomentumScrollBegin={() => {
                  console.log('onMomentumScrollBegin');
                  this.canAction = true;
                }}
                onMomentumScrollEnd={() => {
                  console.log('onMomentumScrollEnd');
                  this.canAction = false;
                }}
            />
            <View style={styles.buttonView}>
                <View style={styles.button}>
                    <Icon size={40} type='material-community' name='image-plus' color={'#03a9f4'} onPress={() => this._pickImage()} />
                </View>
            </View>

        </SafeAreaView>
    );
}

我便同时拥有这两个功能

    _onRefresh = () => {
    console.log('_onRefresh');
    if(!this.canAction) return;
    console.log('_onRefresh INSIDE');
    };

  _onEndReached = () => {
    console.log('_onEndReached');
    if(!this.canAction) return;
    console.log('_onEndReached INSIDE');
  };

预期它应该从我获得的结果中的任何一个函数中打印出一些东西

尝试A。

render() {
    return (

        <SafeAreaView style={styles.gridView}>
            <FlatList
                data={formatData(data)}
                renderItem={this.renderItem}
                keyExtractor={(item, index) => index.toString()}
                onEndReached={this.loadApp}
                onEndReachedThreshold={0}
                refreshing={this.state.getUpdatedData}
                onRefresh={this.handleUpdateData}
            />
        </SafeAreaView>
    );
}

功能开始

handleUpdateData = async () => {
    this.setState({
        getUpdatedData: true
    }, () => {
        AsyncStorage.getItem('U_LOCALID').then((value) => {

            //remove "" from the string
            LocalID = value.replace(/"/g, '');

            var string = 'getstatsnopic/'
            var data_send = {
                UID: LocalID,
                COUNT: this.state.Count
            }

            Wkr.POST(string, data_send).then(response => {

                //9 - SIGN IN PASSWORD MATCHES
                if (response.D == 9) {
                    //data = response.X;
                    // for (let zz = 0; zz < response.X.length; zz++) {
                    //     //double check that this user is allowed to see this information
                    //     if (LocalID == response.X[zz].UID) {
                    //         objIndex = data.findIndex((obj => obj.PICID == response.X[zz].PICID));

                    //         //Log object to Console.
                    //         console.log("Before update: ", data[objIndex])

                    //         //Update object's name property.
                    //         data[objIndex].ONE = { M: response.X[zz].ONE.M, F: response.X[zz].ONE.F },
                    //         data[objIndex].TWO = { M: response.X[zz].TWO.M, F: response.X[zz].TWO.F },
                    //         data[objIndex].THREE = { M: response.X[zz].THREE.M, F: response.X[zz].THREE.F },
                    //         data[objIndex].FOUR = { M: response.X[zz].FOUR.M, F: response.X[zz].FOUR.F },
                    //         data[objIndex].FIVE = { M: response.X[zz].FIVE.M, F: response.X[zz].FIVE.F },
                    //         data[objIndex].SIX = { M: response.X[zz].SIX.M, F: response.X[zz].SIX.F },
                    //         data[objIndex].SEVEN = { M: response.X[zz].SEVEN.M, F: response.X[zz].SEVEN.F },
                    //         data[objIndex].EIGHT = { M: response.X[zz].EIGHT.M, F: response.X[zz].EIGHT.F },
                    //         data[objIndex].NINE = { M: response.X[zz].NINE.M, F: response.X[zz].NINE.F },

                    //         //Log object to console again.
                    //         console.log("After update: ", data[objIndex])


                    //     } else {
                    //         console.log('You Should Not Have Gotten This Data')
                    //     }
                    // }
                    this.setState({
                        Count: this.state.Count + 5,
                        refresh: this.state.refresh + 1,
                        getUpdatedData: false
                    })
                }
                //7 - ERROR
                else if (response.D == 7) {
                    console.log("NOTHING");
                    this.setState({ getUpdatedData: false })
                }
                //0 - ERROR
                else if (response.D == 0) {
                    console.log("ERROR");
                } else {
                    console.log("SHOULD NOT BEEN HERE");
                }
            });
        })
    })

}

B实际结果。

[17:30:13] onScrollBeginDrag
[17:30:13] onScrollEndDrag
[17:30:13] onMomentumScrollBegin
[17:30:13] onMomentumScrollEnd
[17:30:14] onScrollBeginDrag
[17:30:14] onScrollEndDrag
[17:30:14] onMomentumScrollBegin
[17:30:14] onMomentumScrollEnd
[17:30:14] onScrollBeginDrag
[17:30:15] onScrollEndDrag
[17:30:15] onMomentumScrollBegin
[17:30:15] onMomentumScrollEnd
[17:30:15] onScrollBeginDrag
[17:30:16] onScrollEndDrag
[17:30:16] onMomentumScrollBegin
[17:30:17] onMomentumScrollEnd
[17:30:17] onScrollBeginDrag
[17:30:17] onScrollEndDrag
[17:30:17] onMomentumScrollBegin
[17:30:18] onMomentumScrollEnd

预期的B结果。

应该打给

_onEndReached or  _onRefresh

尝试实际结果。

[17:37:43] NOTHING
[17:37:45] NOTHING
[17:37:45] NOTHING
[17:37:45] NOTHING
[17:37:45] NOTHING
[17:37:45] NOTHING
[17:37:45] NOTHING
[17:37:45] NOTHING

预期的结果。

应该打一次电话

NOTHING

所以在B中,我认为尝试某种bool应该或可以有所帮助,但什至不起作用。

在A中它可以工作,但是它的调用方式很多次。

0 个答案:

没有答案