Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

prefetching.md

51 lines1.1 KB

1---
2id: prefetching
3title: Prefetching & Router Integration
4ref: docs/framework/react/guides/prefetching.md
5replace: { 'react-query': 'preact-query', 'React': 'Preact' }
6---
7​
8[//]: # 'ExampleConditionally1'
9​
10```tsx
11// This lazy loads the GraphFeedItem component, meaning
12// it wont start loading until something renders it
13import { lazy } from 'preact/iso'
14​
15const GraphFeedItem = lazy(() => import('./GraphFeedItem'))
16​
17function Feed() {
18 const { data, isPending } = useQuery({
19 queryKey: ['feed'],
20 queryFn: getFeed,
21 })
22​
23 if (isPending) {
24 return 'Loading feed...'
25 }
26​
27 return (
28 <>
29 {data.map((feedItem) => {
30 if (feedItem.type === 'GRAPH') {
31 return <GraphFeedItem key={feedItem.id} feedItem={feedItem} />
32 }
33​
34 return <StandardFeedItem key={feedItem.id} feedItem={feedItem} />
35 })}
36 </>
37 )
39​
40// GraphFeedItem.tsx
41function GraphFeedItem({ feedItem }) {
42 const { data, isPending } = useQuery({
43 queryKey: ['graph', feedItem.id],
44 queryFn: getGraphDataById,
45 })
46​
47 ...
49```
50​
51[//]: # 'ExampleConditionally1'