首次加载页面时多次调用onReachedEnd方法

来源:2-

Leal_朝

2019-04-18

老师, 我跟着你的视频敲的,代码相同, 发现有个小问题: 页面在首次加载进来时会自动触发onReachedEnd,比如我初始化加载2条数据,onReachedEnd调用的方法会加多2条数据,那么首次加载页面时会在初始化的2条数据的基础上在2s后加多2条,2s后再加多2条,重复4-5次, 请问这个是什么原理呢? 何解?

写回答

1回答

CrazyCodeBoy

2019-04-18

这个在后面的课程中有讲解哦,建议参考下:

render() {
    let store = this._store();
    const {theme}=this.props;
    return (
        <View style={styles.container}>
            <FlatList
                data={store.projectModels}
                renderItem={data => this.renderItem(data)}
                keyExtractor={item => "" + item.item.id}
                refreshControl={
                    <RefreshControl
                        title={'Loading'}
                        titleColor={theme.themeColor}
                        colors={[theme.themeColor]}
                        refreshing={store.isLoading}
                        onRefresh={() => this.loadData()}
                        tintColor={theme.themeColor}
                    />
                }
                ListFooterComponent={() => this.genIndicator()}
                onEndReached={() => {
                    console.log('---onEndReached----');
                    setTimeout(() => {
                        if (this.canLoadMore) {//fix 滚动时两次调用onEndReached https://github.com/facebook/react-native/issues/14015
                            this.loadData(true);
                            this.canLoadMore = false;
                        }
                    }, 100);
                }}
                onEndReachedThreshold={0.5}
                onMomentumScrollBegin={() => {
                    this.canLoadMore = true; //fix 初始化时页调用onEndReached的问题
                    console.log('---onMomentumScrollBegin-----')
                }}
            />
            <Toast ref={'toast'}
                   position={'center'}
            />
        </View>
    );
}

https://git.imooc.com/coding-304/GitHub_Advanced/src/master/js/page/PopularPage.js#L203


1
1
Leal_朝
谢谢老师!回复非常快!
2019-04-18
共1条回复

RN入门到进阶,打造高质量上线App

解锁React Native开发应用新姿势,React Native新版本热门技术

3170 学习 · 3256 问题

查看课程