@@ -5,10 +5,13 @@ import { Callout, Sandpack } from '@/components'
5
5
usePrefetchQuery는 아무것도 반환하지 않으며, useSuspenseQuery를 사용하는 컴포넌트를 래핑하는 suspense boundary가 렌더 되기전에 prefetch를 발생시키는 데에 사용합니다.
6
6
7
7
``` jsx /usePrefetchQuery/
8
- import { usePrefetchQuery , queryOptions } from ' @suspensive/react-query'
8
+ import { usePrefetchQuery , useSuspenseQuery } from ' @suspensive/react-query'
9
9
10
10
const PostPage = ({ postId }) => {
11
- usePrefetchQuery (query .post (postId)) // suspense경계 전에 prefetch를 발생 시키는 데에 사용합니다
11
+ usePrefetchQuery ({
12
+ queryKey: [' posts' , postId],
13
+ queryFn : () => getPost (postId),
14
+ }) // suspense경계 전에 prefetch를 발생 시키는 데에 사용합니다
12
15
13
16
return (
14
17
< Suspense fallback= {< div> Loading... < / div> }>
@@ -18,37 +21,28 @@ const PostPage = ({ postId }) => {
18
21
}
19
22
20
23
export const Post = ({ postId }) => {
21
- const { data } = useSuspenseQuery (query .post (postId))
24
+ const { data } = useSuspenseQuery ({
25
+ queryKey: [' posts' , postId],
26
+ queryFn : () => getPost (postId),
27
+ })
22
28
23
- return (
24
- < div>
25
- < h1> {data .title }< / h1>
26
- < p> {data .body }< / p>
27
- < / div>
28
- )
29
- }
30
-
31
- const query = {
32
- post : (postId ) =>
33
- queryOptions ({
34
- queryKey: [' posts' , postId],
35
- queryFn : () => getPost (postId),
36
- }),
29
+ return <> ... < / >
37
30
}
38
31
```
39
32
40
33
<Sandpack >
41
34
42
35
``` tsx Example.tsx active
43
36
import { Suspense } from ' @suspensive/react'
44
- import { usePrefetchQuery } from ' @suspensive/react-query'
45
- import React from ' react'
46
- import { Post } from ' ./Post'
47
- import { query } from ' ./query'
37
+ import { usePrefetchQuery , useSuspenseQuery } from ' @suspensive/react-query'
38
+ import { useState } from ' react'
48
39
49
40
export const Example = () => {
50
- const [postId, setPostId] = React .useState (1 )
51
- usePrefetchQuery (query .post (postId ))
41
+ const [postId, setPostId] = useState (1 )
42
+ usePrefetchQuery ({
43
+ queryKey: [' posts' , postId ],
44
+ queryFn :() => getPost (postId ),
45
+ }) // suspense경계 전에 prefetch를 발생 시키는 데에 사용합니다
52
46
53
47
return (
54
48
<div >
@@ -64,14 +58,12 @@ export const Example = () => {
64
58
</div >
65
59
)
66
60
}
67
- ```
68
61
69
- ``` tsx Post.tsx
70
- import { useSuspenseQuery } from ' @suspensive/react-query'
71
- import { query } from ' ./query'
72
-
73
- export const Post = ({ postId }: { postId: number }) => {
74
- const { data } = useSuspenseQuery (query .post (postId ))
62
+ const Post = ({ postId }: { postId: number }) => {
63
+ const { data } = useSuspenseQuery ({
64
+ queryKey: [' posts' , postId ],
65
+ queryFn :() => getPost (postId ),
66
+ })
75
67
76
68
return (
77
69
<div >
@@ -80,28 +72,11 @@ export const Post = ({ postId }: { postId: number }) => {
80
72
</div >
81
73
)
82
74
}
83
- ```
84
75
85
- ``` tsx /query.ts
86
- import { queryOptions } from ' @suspensive/react-query'
87
-
88
- export const query = {
89
- post : (postId : number ) =>
90
- queryOptions ({
91
- queryKey: [' posts' , postId ],
92
- queryFn : async (): Promise <{
93
- id: number
94
- title: string
95
- body: string
96
- }> => {
97
- const response = await fetch (` https://jsonplaceholder.typicode.com/posts/${postId } ` )
98
- if (! response .ok ) {
99
- throw new Error (' An error occurred' )
100
- }
101
- return await response .json ()
102
- }
103
- })
104
- }
76
+ const getPost = (postId : number ) =>
77
+ fetch (` https://jsonplaceholder.typicode.com/posts/${postId } ` ).then <{ id: number ; title: string ; body: string }>(
78
+ (res ) => res .json ()
79
+ )
105
80
```
106
81
107
82
</Sandpack >
0 commit comments