Skip to content

Commit 83512ea

Browse files
committed
add data prefetching
1 parent 1508d6d commit 83512ea

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

apps/react-vite/src/features/discussions/components/discussions-list.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1+
import { useQueryClient } from '@tanstack/react-query';
2+
13
import { Link } from '@/components/ui/link';
24
import { Spinner } from '@/components/ui/spinner';
35
import { Table } from '@/components/ui/table';
46
import { formatDate } from '@/utils/format';
57

8+
import { getDiscussionQueryOptions } from '../api/get-discussion';
69
import { useDiscussions } from '../api/get-discussions';
710

811
import { DeleteDiscussion } from './delete-discussion';
912

1013
export const DiscussionsList = () => {
1114
const discussionsQuery = useDiscussions();
15+
const queryClient = useQueryClient();
1216

1317
if (discussionsQuery.isLoading) {
1418
return (
@@ -39,7 +43,17 @@ export const DiscussionsList = () => {
3943
title: '',
4044
field: 'id',
4145
Cell({ entry: { id } }) {
42-
return <Link to={`./${id}`}>View</Link>;
46+
return (
47+
<Link
48+
onMouseEnter={() => {
49+
// Prefetch the discussion data when the user hovers over the link
50+
queryClient.prefetchQuery(getDiscussionQueryOptions(id));
51+
}}
52+
to={`./${id}`}
53+
>
54+
View
55+
</Link>
56+
);
4357
},
4458
},
4559
{

docs/performance.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,9 @@ Use `srcset` to load the most optimal image for the clients screen size.
8787
### Web vitals
8888

8989
Since Google started taking web vitals in account when indexing websites, you should keep an eye on web vitals scores from [Lighthouse](https://web.dev/measure/) and [Pagespeed Insights](https://pagespeed.web.dev/).
90+
91+
### Data prefetching
92+
93+
It is possible to prefetch data before the user navigates to a page. This can be done by using the `queryClient.prefetchQuery` method from the `@tanstack/react-query` library. This method allows you to prefetch data for a specific query. This can be useful when you know that the user will navigate to a specific page and you want to prefetch the data before the user navigates to the page. This can help to improve the performance of the application by reducing the time it takes to load the data when the user navigates to the page.
94+
95+
[Data Prefetching Example Code](../apps/react-vite/src/features/discussions/components/discussions-list.tsx)

0 commit comments

Comments
 (0)