Skip to content

Commit 2ce0e33

Browse files
committed
paginated queries
1 parent 2eb44ed commit 2ce0e33

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ Dependent query - is dependent on the results of another query.
100100

101101
On react-query heroes page we have displayed different heroes. If we click on the hero, we see `loading` text for a moment. To get rid of that initial loading statement, we can set `initialData` in useSuperHeroData.tsx file. If we refresh detail page, the loading button will show because we don't have any initial data at the start, because we skipped the fetch all super heroes query.
102102

103+
**12. react-query keepPreviousData - Paginated queries**
104+
105+
If we add flag `keepPreviousData` into the useQuery options, when we paginate to next page, the data from the previous page is still displayed till the new data is successfully fetched.
106+
103107
## Available Scripts
104108

105109
In the project directory, you can run:

db.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,39 @@
4141
"id": "codevolution",
4242
"courses": ["react", "vue", "angular"]
4343
}
44+
],
45+
"colors": [
46+
{
47+
"id": 1,
48+
"label": "red"
49+
},
50+
{
51+
"id": 2,
52+
"label": "blue"
53+
},
54+
{
55+
"id": 3,
56+
"label": "green"
57+
},
58+
{
59+
"id": 4,
60+
"label": "yellow"
61+
},
62+
{
63+
"id": 5,
64+
"label": "black"
65+
},
66+
{
67+
"id": 6,
68+
"label": "white"
69+
},
70+
{
71+
"id": 7,
72+
"label": "orange"
73+
},
74+
{
75+
"id": 8,
76+
"label": "purple"
77+
}
4478
]
4579
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import axios from 'axios'
2+
import { FC, useState } from 'react'
3+
import { useQuery } from 'react-query'
4+
5+
interface Props {}
6+
7+
const fetchColors = (pageNumber: number) => {
8+
return axios.get(`http://localhost:4000/colors?_limit=2&_page=${pageNumber}`)
9+
}
10+
11+
const PaginatedQueries: FC<Props> = (props: Props) => {
12+
const [pageNumber, setPageNumber] = useState<number>(1)
13+
const { data, isLoading, isError, error, isFetching } = useQuery(
14+
['colors', pageNumber],
15+
() => fetchColors(pageNumber),
16+
{
17+
keepPreviousData: true,
18+
},
19+
)
20+
21+
if (isLoading) {
22+
return <h2>Loading...</h2>
23+
}
24+
25+
if (isError && error instanceof Error) {
26+
return <h2>{error.message}</h2>
27+
}
28+
return (
29+
<div className="paginated-queries">
30+
<h1>PaginatedQueries</h1>
31+
{data?.data.map((color: { id: number; label: string }, index: number) => (
32+
<div key={index}>
33+
<h2>
34+
{color.id}. {color.label}
35+
</h2>
36+
</div>
37+
))}
38+
<div>
39+
<button onClick={() => setPageNumber((prev) => prev - 1)} disabled={pageNumber === 1}>
40+
Prev page
41+
</button>
42+
<button onClick={() => setPageNumber((prev) => prev + 1)} disabled={pageNumber === 4}>
43+
Next page
44+
</button>
45+
{isFetching && 'loading'}
46+
</div>
47+
</div>
48+
)
49+
}
50+
51+
export default PaginatedQueries

src/routes/Routes.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import RQSuperHero from 'pages/RQSuperHero/RQSuperHero'
99
import ParallelQueries from 'pages/ParallelQueries/ParallelQueries'
1010
import DynamicParallelQueries from 'pages/DynamicParallelQueries/DynamicParallelQueries'
1111
import DependentQueries from 'pages/DependentQueries/DependentQueries'
12+
import PaginatedQueries from 'pages/PaginatedQueries/PaginatedQueries'
1213

1314
export enum RouteType {
1415
PUBLIC,
@@ -57,6 +58,11 @@ export const AppRoutes: AppRoute[] = [
5758
path: '/dependent-queries',
5859
children: <DependentQueries email="[email protected]" />,
5960
},
61+
{
62+
type: RouteType.PUBLIC,
63+
path: '/paginated-queries',
64+
children: <PaginatedQueries />,
65+
},
6066
]
6167

6268
const Routes: FC = () => {

0 commit comments

Comments
 (0)