-
Notifications
You must be signed in to change notification settings - Fork 0
/
Home.js
42 lines (35 loc) · 1.01 KB
/
Home.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import React from "react"
import { StatusBar } from "expo-status-bar"
import { View, StyleSheet, FlatList } from "react-native"
import { useInfiniteQuery } from "react-query"
import PokemonCard from "./PokemonCard"
const fetchPokemon = (_, cursor) => {
return fetch(cursor || "https://pokeapi.co/api/v2/pokemon/")
.then((res) => res.json())
.catch(console.log)
}
const Home = () => {
const { data, canFetchMore, fetchMore } = useInfiniteQuery(
"pokemon",
fetchPokemon,
{
getFetchMore: (lastPage) => lastPage.next,
}
)
return (
<View>
<StatusBar style="light-content" />
<FlatList
data={data
?.map((page) => page.results)
.reduce((all, page) => [...all, ...page])}
renderItem={({ item }) => <PokemonCard {...item} />}
keyExtractor={(item) => item.name}
onEndReachedThreshold={5}
onEndReached={() => canFetchMore && fetchMore()}
/>
</View>
)
}
const styles = StyleSheet.create({})
export default Home