gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 20:55:08 25 4
gpt4 key购买 nike

我正在尝试向 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}
/>

page更改,我运行 GraphQL 查询:
    const {loading} = useQuery(MY_GROUPS, {
variables: {limit: 10, page},
fetchPolicy: "network-only",
onCompleted: data => {
setPickupsGroups(pickupsGroups.concat(data.myPickupGroups.pickupGroups))
}
})

但每次我向 pickupGroups 添加新项目时, FlatList 滚动回顶部

最佳答案

实际上,当您在 onCompleted 方法中调用 setPickupsGroups 方法时,它会导致屏幕重新渲染,尝试为 pickUpGroups 使用全局变量,或者如果您使用的是 apollo graphQL,则从您的 graphQL 服务器尝试发送 hasMore,并在应用程序端调用 fetchMore 更新查询不会导致您的屏幕重新渲染并且不会滚动到顶部..

关于javascript - React Native FlatList 添加新项目时跳到顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61209715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com