Skip to content

Commit 21b8ad4

Browse files
committed
Add RN FlatList example
1 parent 19ca596 commit 21b8ad4

File tree

4 files changed

+74
-12
lines changed

4 files changed

+74
-12
lines changed

examples/query/react/infinite-queries/src/App.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import LimitOffsetExample from "./features/limit-offset/LimitOffsetExample"
1111
import { InfiniteScrollMaxPagesExample } from "./features/max-pages/InfiniteScrollMaxExample"
1212
import PaginationInfScrollExample from "./features/pagination-infinite-scroll/PaginationInfScrollExample"
1313
import { PaginationExample } from "./features/pagination/PaginationExample"
14+
import { FlatlistExample } from "./features/rn-flatlist/FlatlistExample"
1415

1516
const Menu = () => {
1617
return (
@@ -43,6 +44,9 @@ const Menu = () => {
4344
Pagination Infinite Scroll
4445
</Link>
4546
</li>
47+
<li>
48+
<Link to="/examples/rn-flatlist">RN FlatList</Link>
49+
</li>
4650
</ul>
4751
</div>
4852
)
@@ -84,6 +88,7 @@ const App = () => {
8488
path="pagination-infinite-scroll"
8589
element={<PaginationInfScrollExample />}
8690
/>
91+
<Route path="rn-flatlist" element={<FlatlistExample />} />
8792
</Route>
8893
</Routes>
8994
</div>

examples/query/react/infinite-queries/src/features/infinite-scroll/InfiniteScrollExample.tsx

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useInView } from "react-intersection-observer"
33
import { Link } from "react-router"
44

55
import { apiWithInfiniteScroll } from "./infiniteScrollApi"
6+
import type { Project } from "./infiniteScrollApi"
67

78
export const InfiniteScrollAbout = () => {
89
return (
@@ -18,6 +19,22 @@ export const InfiniteScrollAbout = () => {
1819
)
1920
}
2021

22+
export const ProjectRow = ({ project }: { project: Project }) => {
23+
return (
24+
<p
25+
style={{
26+
border: "1px solid gray",
27+
borderRadius: "5px",
28+
padding: "5rem 1rem",
29+
background: `hsla(${project.id * 30}, 60%, 80%, 1)`,
30+
}}
31+
key={project.id}
32+
>
33+
{project.name}
34+
</p>
35+
)
36+
}
37+
2138
export const InfiniteScrollExample = () => {
2239
const {
2340
data,
@@ -67,17 +84,7 @@ export const InfiniteScrollExample = () => {
6784
{data?.pages.map(page => (
6885
<React.Fragment key={page.nextId}>
6986
{page.projects.map(project => (
70-
<p
71-
style={{
72-
border: "1px solid gray",
73-
borderRadius: "5px",
74-
padding: "5rem 1rem",
75-
background: `hsla(${project.id * 30}, 60%, 80%, 1)`,
76-
}}
77-
key={project.id}
78-
>
79-
{project.name}
80-
</p>
87+
<ProjectRow key={project.id} project={project} />
8188
))}
8289
</React.Fragment>
8390
))}

examples/query/react/infinite-queries/src/features/infinite-scroll/infiniteScrollApi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { baseApi } from "../baseApi"
22

3-
type Project = {
3+
export type Project = {
44
id: number
55
name: string
66
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { ActivityIndicator, FlatList, View, Text } from "react-native-web"
2+
import { useMemo } from "react"
3+
4+
import { apiWithInfiniteScroll } from "../infinite-scroll/infiniteScrollApi"
5+
import { ProjectRow } from "../infinite-scroll/InfiniteScrollExample"
6+
7+
export const FlatlistExample = () => {
8+
const {
9+
data,
10+
error,
11+
fetchNextPage,
12+
fetchPreviousPage,
13+
hasNextPage,
14+
isFetchingNextPage,
15+
isLoading,
16+
isFetching,
17+
isError,
18+
// refetch,
19+
} =
20+
apiWithInfiniteScroll.endpoints.getProjectsCursor.useInfiniteQuery(
21+
"projects",
22+
)
23+
24+
const allProjects = useMemo(() => {
25+
return data?.pages.flatMap(page => page.projects) ?? []
26+
}, [data])
27+
28+
return (
29+
<>
30+
<h2>React Native FlatList Example</h2>
31+
<View style={{ width: "100%", maxHeight: "600px" }}>
32+
{isLoading ? (
33+
<ActivityIndicator />
34+
) : isError ? (
35+
<Text>{error?.message}</Text>
36+
) : (
37+
<FlatList
38+
data={allProjects}
39+
keyExtractor={item => item.id.toString()}
40+
renderItem={({ item }) => <ProjectRow project={item} />}
41+
// onRefresh={refetch}
42+
refreshing={isLoading}
43+
progressViewOffset={100}
44+
onEndReached={() => fetchNextPage()}
45+
/>
46+
)}
47+
</View>
48+
</>
49+
)
50+
}

0 commit comments

Comments
 (0)