简体   繁体   English

React Native FlatList 添加新项目时跳到顶部

[英]React Native FlatList jumps to the top when adding new items

I'm trying to add an Infinite Scroll feature to a FlatList:我正在尝试向 FlatList 添加无限滚动功能:

<FlatList
   data={pickupsGroups}
   renderItem={({item , index}: {item: PickupGroup, index: number}) => {
                    return <PickingCard group={item} onPress={onPressPickup}/>
   }}
   onRefresh={onRefresh}
   keyExtractor={item => `${item?.title}-${item?.totalSku}`}
   refreshing={refreshing}
   onEndReached={() => {
     setPage(page + 1)
   }}
   onEndReachedThreshold={0.1}
/>

when the page changes, I run a GraphQL query:page更改时,我运行 GraphQL 查询:

    const {loading} = useQuery(MY_GROUPS, {
        variables: {limit: 10, page},
        fetchPolicy: "network-only",
        onCompleted: data => {
            setPickupsGroups(pickupsGroups.concat(data.myPickupGroups.pickupGroups))
        }
    })

But every time I add new items to pickupGroups , the FlatList scrolls back to the top但是每次我将新项目添加到pickupGroups时,FlatList 都会滚动回顶部

Actually when you call method setPickupsGroups in onCompleted Method, it causes screen to re-render, try using a global variable for pickUpGroups, or if you are using apollo graphQL, then from your graphQL server try sending hasMore, and in app side call fetchMore Update Query which will not cause your screen to re-render and it won't scroll to top..实际上,当您在 onCompleted 方法中调用 setPickupsGroups 方法时,它会导致屏幕重新渲染,尝试使用全局变量进行 pickUpGroups,或者如果您使用的是 apollo graphQL,那么从您的 graphQL 服务器尝试发送 hasMore,并在应用程序端调用 fetchMore 更新查询不会导致您的屏幕重新渲染并且不会滚动到顶部..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM