React Native nested ListView triggers onEndReached multiple times on loading

若如初见. 提交于 2019-12-07 10:27:33

问题


Here's the code:

<ScrollView>
          { tree.myPoiComments.CommentInfo && tree.myPoiComments.CommentInfo.length>0 &&
          <FlatList
            data={tree.myPoiComments.CommentInfo}
            keyExtractor = {(item, index) => item.CommentId}
            ListHeaderComponent = {() => <View>
                      <Text style={styles.listHeader}>My Comments</Text>
                      </View>}
            renderItem= {({item}) => <CommentItem comment={item} owner={1} />}
          />
          }
          { tree.poiComments.CommentInfo && tree.poiComments.CommentInfo.length>0 &&
          <FlatList
            data={tree.poiComments.CommentInfo}
            keyExtractor = {(item, index) => item.CommentId}
            onEndReachedThreshold={1}
            onEndReached={(info) => {
            alert(JSON.stringify(info));
            } }
            extraData = {this.state}
            bounces={false}
            ListHeaderComponent = {() => <View>
                      <Text style={styles.listHeader}>People's Comments</Text>
                      </View>}
            renderItem= {({item}) => <CommentItem comment={item} owner={0} />}
          />
          }
        </ScrollView>

I've went through the issue list in github of react native. onEndReached just won't work properly when there is a ScrollView wrapped. I tried everything I find, none worked.

I only need the FlatList displaying data. Two lists scroll separately is not what I want. That's why I need the ScrollView. It seems the nesting construction is inevitable. Is there a way around?


回答1:


This is how my flat list looks like and it works fine.

https://github.com/facebook/react-native/issues/16067

    <FlatList
        onEndReachedThreshold={ 0.5 }
        onEndReached={ () => this.onEndReached() }
        refreshing= { false }
        onRefresh={ ()=> {
            this.refetchData()
        } }
        data={this.state.data}
        renderItem={({ item }) => <Item  id={item.key} />} />


来源:https://stackoverflow.com/questions/48940820/react-native-nested-listview-triggers-onendreached-multiple-times-on-loading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!