【发布时间】:2021-12-01 06:16:55
【问题描述】:
我的水平FlatList 可以很好地处理虚拟数据,但是当我尝试将数据从后端传递到我的FlatList 时,它返回错误scrollToIndex out of range: requested index -1 but minimum is 0。
数据在垂直FlatList 中正确传递,但是当我尝试使用水平数据时,它给出了这个错误。
您认为我的水平FlatList 代码有什么问题?
useEffect(() => {
const getIndex = (placeholder = 0) => {
const index = places.findIndex(place => place.id === selectedPlaceId);
if (index === -1) return placeholder;
return index;
};
const index = getIndex();
flatlist.current.scrollToIndex({index});
const selectedPlace = places[index];
const region = {
latitude: selectedPlace.coordinate.latitude,
longitude: selectedPlace.coordinate.longitude,
latitudeDelta: 0.8,
longitudeDelta: 0.8
}
map.current.animateToRegion(region);
}, [selectedPlaceId])
return (
<FlatList
ref={flatlist}
data={trips}
renderItem={({ item }) => <PostCarouselItem post={item} />}
horizontal
showsHorizontalScrollIndicator={false}
snapToInterval={width - 70}
snapToAlignment={"center"}
decelerationRate={"fast"}
viewabilityConfig={viewConfig.current}
onViewableItemsChanged={onViewChanged.current}
style={{ bottom: 10, left: 0, right: 0, position: "absolute", }}
contentContainerStyle={{ marginHorizontal: cardWidth }}
ListFooterComponent={<View style={{marginRight: (cardWidth * 2)}}/>}
/>
);
【问题讨论】:
标签: javascript react-native react-native-flatlist