사용자가 페이지 하단에 도달했을 때, 콘텐츠가 계속 로드되는 방식
import { View, Text, FlatList, ActivityIndicator } from "react-native";
import { useInfiniteQuery } from "react-query";
import { getData } from "./api";
import List from "./List";
const App = () => {
const {
isLoading,
isError,
data,
error,
fetchNextPage,
hasNextPage
} = useInfiniteQuery("lists", getData, {
getNextPageParam: (currentPage) => {
// 데이터마다 다름
const nextPage = currentPage.page + 1;
return nextPage > currentPage.total_pages ? null : nextPage;
},
});
if (isLoading) {
return (
<>
<ActivityIndicator />
</>
);
}
if (isError) return <Text>에러: {error.message}</Text>;
const loadMore = () => {
if (hasNextPage) fetchNextPage();
};
return (
<>
<View style={{ margin: 20 }}>
<Text style={{ fontSize: 30, color: isDark ? "white" : "black" }}>Lists</Text>
</View>
<FlatList
onEndReached={loadMore}
onEndReachedThreshold={0.6}
data={data}
renderItem={({ item }) => <List item={item} />}
keyExtractor={(item) => item.id}
ItemSeparatorComponent={<View style={{ height: 10 }} />}
/>
</>
);
};
export default App;