Skip to content

Commit f6877ba

Browse files
authored
Query examples: Prefetching (reduxjs#1105)
* Add prefetching examples * Updated CSB links in docs
1 parent c87544b commit f6877ba

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+53747
-13
lines changed

docs/rtk-query/usage/prefetching.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ dispatch(api.endpoints[endpointName].initiate(arg, { forceRefetch: true }))
119119
This is a very basic example that shows how you can prefetch when a user hovers over the next arrow. This is probably not the optimal solution, because if they hover, click, then change pages without moving their mouse, we wouldn't know to prefetch the next page because we wouldn't see the next `onMouseEnter` event. In this case, you would need to handle this on your own. You could also consider automatically prefetching the next page...
120120

121121
<iframe
122-
src="https://codesandbox.io/embed/concepts-prefetching-h594j?fontsize=12&hidenavigation=1&theme=dark"
122+
src="https://codesandbox.io/embed/github/reduxjs/redux-toolkit/tree/feature/v1.6-integration/examples/query/react/prefetching?fontsize=12&hidenavigation=1&theme=dark"
123123
style={{
124124
width: '100%',
125125
height: '600px',
@@ -137,7 +137,7 @@ This is a very basic example that shows how you can prefetch when a user hovers
137137
Picking up on our last example, we automatically `prefetch` the next page, giving the appearance of no network delay.
138138

139139
<iframe
140-
src="https://codesandbox.io/embed/concepts-prefetching-automatic-2id61?fontsize=12&hidenavigation=1&theme=dark"
140+
src="https://codesandbox.io/embed/github/reduxjs/redux-toolkit/tree/feature/v1.6-integration/examples/query/react/prefetching-automatic?fontsize=12&hidenavigation=1&theme=dark"
141141
style={{
142142
width: '100%',
143143
height: '600px',
@@ -155,7 +155,7 @@ Picking up on our last example, we automatically `prefetch` the next page, givin
155155
After the first query initialized by `useQuery` runs, we automatically fetch all remaining pages.
156156

157157
<iframe
158-
src="https://codesandbox.io/embed/concepts-prefetching-automatic-waterfall-ihe5e?fontsize=12&hidenavigation=1&theme=dark&module=%2Fsrc%2Ffeatures%2Fposts%2FPostsManager.tsx"
158+
src="https://codesandbox.io/embed/github/reduxjs/redux-toolkit/tree/feature/v1.6-integration/examples/query/react/prefetching-automatic-waterfall?fontsize=12&hidenavigation=1&theme=dark&module=%2Fsrc%2Ffeatures%2Fposts%2FPostsManager.tsx"
159159
style={{
160160
width: '100%',
161161
height: '600px',

examples/query/react/mutations/src/features/posts/PostsManager.tsx

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,25 +85,63 @@ const AddPost = () => {
8585
}
8686

8787
const PostList = () => {
88-
const { data: posts, isLoading } = useGetPostsQuery()
89-
const { push } = useHistory()
88+
const [page, setPage] = useState(1)
89+
const { data: posts, isLoading, isFetching } = hooks.listPosts.useQuery(page)
90+
const prefetchPage = usePrefetch('listPosts')
91+
92+
const prefetchNext = useCallback(() => {
93+
prefetchPage(page + 1)
94+
}, [prefetchPage, page])
95+
96+
useEffect(() => {
97+
if (page !== posts?.total_pages) {
98+
prefetchNext()
99+
}
100+
}, [posts, page, prefetchNext])
90101

91102
if (isLoading) {
92103
return <div>Loading</div>
93104
}
94105

95-
if (!posts) {
106+
if (!posts?.data) {
96107
return <div>No posts :(</div>
97108
}
98109

99110
return (
100-
<List spacing={3}>
101-
{posts.map(({ id, name }) => (
102-
<ListItem key={id} onClick={() => push(`/posts/${id}`)}>
103-
<ListIcon as={MdBook} color="green.500" /> {name}
104-
</ListItem>
105-
))}
106-
</List>
111+
<Box>
112+
<HStack spacing="14px">
113+
<Button
114+
onClick={() => setPage((prev) => prev - 1)}
115+
isLoading={isFetching}
116+
disabled={page === 1}
117+
>
118+
<Icon as={MdArrowBack} />
119+
</Button>
120+
<Button
121+
onClick={() => setPage((prev) => prev + 1)}
122+
isLoading={isFetching}
123+
disabled={page === posts.total_pages}
124+
onMouseEnter={prefetchNext}
125+
>
126+
<Icon as={MdArrowForward} />
127+
</Button>
128+
<Box>{`${page} / ${posts.total_pages}`}</Box>
129+
</HStack>
130+
<List spacing={3} mt={6}>
131+
{posts?.data.map(({ id, title, status }) => (
132+
<ListItem key={id}>
133+
<ListIcon as={MdBook} color="green.500" /> {title}{' '}
134+
<Badge
135+
ml="1"
136+
fontSize="0.8em"
137+
colorScheme={getColorForStatus(status)}
138+
>
139+
{status}
140+
</Badge>
141+
</ListItem>
142+
))}
143+
</List>
144+
</Box>
107145
)
108146
}
109147

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SKIP_PREFLIGHT_CHECK=true

0 commit comments

Comments
 (0)