Skip to content

Commit 1848a2e

Browse files
manudeligwansikkkangju2000
authored
docs(react-query): add docs for PrefetchQuery (#1297)
related with #1253 # Overview <!-- A clear and concise description of what this pr is about. --> I added docs for PrefetchQuery I added @gwansikk @kangju2000 as co-author ## PR Checklist - [x] I did below actions if need 1. I read the [Contributing Guide](https://github.com/toss/suspensive/blob/main/CONTRIBUTING.md) 2. I added documents and tests. --------- Co-authored-by: Gwansik Kim <[email protected]> Co-authored-by: Juhyeok Kang <[email protected]> Co-authored-by: Gwansik Kim <[email protected]>
1 parent 4cbd183 commit 1848a2e

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { Callout, Sandpack } from '@/components'
2+
3+
# PrefetchQuery
4+
5+
A component that allows you to use usePrefetchQuery in JSX, avoiding the limitations of React hooks.
6+
7+
```jsx /PrefetchQuery/
8+
import { PrefetchQuery, useSuspenseQuery, queryOptions } from '@suspensive/react-query'
9+
import { InView } from '@suspensive/react-dom'
10+
import { useQuery } from '@tanstack/react-query'
11+
12+
const PostsPage = () => {
13+
const { data: posts } = useSuspenseQuery(query.posts())
14+
15+
return posts.map(({ id, title, description }) => (
16+
<InView>
17+
{({ ref, isInView }) => {
18+
return (
19+
<>
20+
{isInView ? (
21+
// 🚫 We can not invoke usePrefetchQuery like below because of React Hook rules
22+
// usePrefetchQuery(query.post(post.id))
23+
24+
// ✅ We can invoke usePrefetch for each post query before entering Post page
25+
<PrefetchQuery {...query.post(id)} />
26+
) : null}
27+
<Link to={`/post/${id}`}>
28+
<div ref={ref}>
29+
<h2>{title}</h2>
30+
<p>{description}</p>
31+
</div>
32+
</Link>
33+
</>
34+
)
35+
}}
36+
</InView>
37+
))
38+
}
39+
40+
const query = {
41+
posts: () =>
42+
queryOptions({
43+
queryKey: ['posts'],
44+
queryFn: () => getPosts(),
45+
}),
46+
post: (postId) =>
47+
queryOptions({
48+
queryKey: ['posts', postId],
49+
queryFn: () => getPost(postId),
50+
}),
51+
}
52+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { Callout, Sandpack } from '@/components'
2+
3+
# PrefetchQuery
4+
5+
React hook의 제약을 피해 JSX상에서도 usePrefetchQuery를 사용할 수 있게 하는 컴포넌트입니다.
6+
7+
```jsx /PrefetchQuery/
8+
import { PrefetchQuery, useSuspenseQuery, queryOptions } from '@suspensive/react-query'
9+
import { InView } from '@suspensive/react-dom'
10+
import { useQuery } from '@tanstack/react-query'
11+
12+
const PostsPage = () => {
13+
const { data: posts } = useSuspenseQuery(query.posts())
14+
15+
return posts.map(({ id, title, description }) => (
16+
<InView>
17+
{({ ref, isInView }) => {
18+
return (
19+
<>
20+
{isInView ? (
21+
// 🚫 React hook의 제약으로 usePrefetchQuery를 사용하지 못합니다
22+
// usePrefetchQuery(query.post(post.id))
23+
24+
// ✅ Post 페이지에 들어가기 전에 각각의 post query에 대해 usePrefetch를 호출할 수 있습니다.
25+
<PrefetchQuery {...query.post(id)} />
26+
) : null}
27+
<Link to={`/post/${id}`}>
28+
<div ref={ref}>
29+
<h2>{title}</h2>
30+
<p>{description}</p>
31+
</div>
32+
</Link>
33+
</>
34+
)
35+
}}
36+
</InView>
37+
))
38+
}
39+
40+
const query = {
41+
posts: () =>
42+
queryOptions({
43+
queryKey: ['posts'],
44+
queryFn: () => getPosts(),
45+
}),
46+
post: (postId) =>
47+
queryOptions({
48+
queryKey: ['posts', postId],
49+
queryFn: () => getPost(postId),
50+
}),
51+
}
52+
```

docs/suspensive.org/src/pages/docs/react-query/_meta.en.json

+1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
"SuspenseQuery": { "title": "<SuspenseQuery/>" },
1717
"SuspenseQueries": { "title": "<SuspenseQueries/>" },
1818
"SuspenseInfiniteQuery": { "title": "<SuspenseInfiniteQuery/>" },
19+
"PrefetchQuery": { "title": "<PrefetchQuery/>" },
1920
"QueryErrorBoundary": { "title": "<QueryErrorBoundary/>" }
2021
}

docs/suspensive.org/src/pages/docs/react-query/_meta.ko.json

+1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
"SuspenseQuery": { "title": "<SuspenseQuery/>" },
1717
"SuspenseQueries": { "title": "<SuspenseQueries/>" },
1818
"SuspenseInfiniteQuery": { "title": "<SuspenseInfiniteQuery/>" },
19+
"PrefetchQuery": { "title": "<PrefetchQuery/>" },
1920
"QueryErrorBoundary": { "title": "<QueryErrorBoundary/>" }
2021
}

0 commit comments

Comments
 (0)