-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(react-query): add docs for usePrefetchQuery (#1296)
related with #1253 # Overview <!-- A clear and concise description of what this pr is about. --> I added docs for usePrefetchQuery of @suspensive/react-query I added @gwansikk(@suspensive/react-query active maintainer) @kangju2000(Sandpack maker) 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]>
- Loading branch information
1 parent
951d702
commit 4cbd183
Showing
4 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
docs/suspensive.org/src/pages/docs/react-query/usePrefetchQuery.en.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { Callout, Sandpack } from '@/components' | ||
|
||
# usePrefetchQuery | ||
|
||
The usePrefetchQuery does not return anything, it should be used just to fire a prefetch during render, before a suspense boundary that wraps a component that uses useSuspenseQuery. | ||
|
||
```jsx /usePrefetchQuery/ | ||
import { usePrefetchQuery, queryOptions } from '@suspensive/react-query' | ||
|
||
const PostPage = ({ postId }) => { | ||
usePrefetchQuery(query.post(postId)) // Prefetch query before suspense boundary | ||
|
||
return ( | ||
<Suspense fallback={<div>Loading...</div>}> | ||
<Post postId={postId} /> | ||
</Suspense> | ||
) | ||
} | ||
|
||
export const Post = ({ postId }) => { | ||
const { data } = useSuspenseQuery(query.post(postId)) | ||
|
||
return ( | ||
<div> | ||
<h1>{data.title}</h1> | ||
<p>{data.body}</p> | ||
</div> | ||
) | ||
} | ||
|
||
const query = { | ||
post: (postId) => | ||
queryOptions({ | ||
queryKey: ['posts', postId], | ||
queryFn: () => getPost(postId), | ||
}), | ||
} | ||
``` | ||
|
||
<Sandpack> | ||
|
||
```tsx Example.tsx active | ||
import { Suspense } from '@suspensive/react' | ||
import { usePrefetchQuery } from '@suspensive/react-query' | ||
import React from 'react' | ||
import { Post } from './Post' | ||
import { query } from './query' | ||
|
||
export const Example = () => { | ||
const [postId, setPostId] = React.useState(1) | ||
usePrefetchQuery(query.post(postId)) | ||
|
||
return ( | ||
<div> | ||
<button onClick={() => setPostId((prev) => prev - 1)} disabled={postId === 1}> | ||
Previous Post | ||
</button> | ||
<button onClick={() => setPostId((prev) => prev + 1)} disabled={postId === 100}> | ||
Next Post | ||
</button> | ||
<Suspense fallback={<div>Loading...</div>}> | ||
<Post postId={postId} /> | ||
</Suspense> | ||
</div> | ||
) | ||
} | ||
``` | ||
|
||
```tsx Post.tsx | ||
import { useSuspenseQuery } from '@suspensive/react-query' | ||
import { query } from './query' | ||
|
||
export const Post = ({ postId }: { postId: number }) => { | ||
const { data } = useSuspenseQuery(query.post(postId)) | ||
|
||
return ( | ||
<div> | ||
<h1>{data.title}</h1> | ||
<p>{data.body}</p> | ||
</div> | ||
) | ||
} | ||
``` | ||
|
||
```tsx /query.ts | ||
import { queryOptions } from '@suspensive/react-query' | ||
|
||
export const query = { | ||
post: (postId: number) => | ||
queryOptions({ | ||
queryKey: ['posts', postId], | ||
queryFn: async (): Promise<{ | ||
id: number | ||
title: string | ||
body: string | ||
}> => { | ||
const response = await fetch(`https://jsonplaceholder.typicode.com/posts/${postId}`) | ||
if (!response.ok) { | ||
throw new Error('An error occurred') | ||
} | ||
return await response.json() | ||
} | ||
}) | ||
} | ||
``` | ||
|
||
</Sandpack> |
107 changes: 107 additions & 0 deletions
107
docs/suspensive.org/src/pages/docs/react-query/usePrefetchQuery.ko.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { Callout, Sandpack } from '@/components' | ||
|
||
# usePrefetchQuery | ||
|
||
usePrefetchQuery는 아무것도 반환하지 않으며, useSuspenseQuery를 사용하는 컴포넌트를 래핑하는 suspense boundary가 렌더 되기전에 prefetch를 발생시키는 데에 사용합니다. | ||
|
||
```jsx /usePrefetchQuery/ | ||
import { usePrefetchQuery, queryOptions } from '@suspensive/react-query' | ||
|
||
const PostPage = ({ postId }) => { | ||
usePrefetchQuery(query.post(postId)) // suspense경계 전에 prefetch를 발생 시키는 데에 사용합니다 | ||
|
||
return ( | ||
<Suspense fallback={<div>Loading...</div>}> | ||
<Post postId={postId} /> | ||
</Suspense> | ||
) | ||
} | ||
|
||
export const Post = ({ postId }) => { | ||
const { data } = useSuspenseQuery(query.post(postId)) | ||
|
||
return ( | ||
<div> | ||
<h1>{data.title}</h1> | ||
<p>{data.body}</p> | ||
</div> | ||
) | ||
} | ||
|
||
const query = { | ||
post: (postId) => | ||
queryOptions({ | ||
queryKey: ['posts', postId], | ||
queryFn: () => getPost(postId), | ||
}), | ||
} | ||
``` | ||
|
||
<Sandpack> | ||
|
||
```tsx Example.tsx active | ||
import { Suspense } from '@suspensive/react' | ||
import { usePrefetchQuery } from '@suspensive/react-query' | ||
import React from 'react' | ||
import { Post } from './Post' | ||
import { query } from './query' | ||
|
||
export const Example = () => { | ||
const [postId, setPostId] = React.useState(1) | ||
usePrefetchQuery(query.post(postId)) | ||
|
||
return ( | ||
<div> | ||
<button onClick={() => setPostId((prev) => prev - 1)} disabled={postId === 1}> | ||
Previous Post | ||
</button> | ||
<button onClick={() => setPostId((prev) => prev + 1)} disabled={postId === 100}> | ||
Next Post | ||
</button> | ||
<Suspense fallback={<div>Loading...</div>}> | ||
<Post postId={postId} /> | ||
</Suspense> | ||
</div> | ||
) | ||
} | ||
``` | ||
|
||
```tsx Post.tsx | ||
import { useSuspenseQuery } from '@suspensive/react-query' | ||
import { query } from './query' | ||
|
||
export const Post = ({ postId }: { postId: number }) => { | ||
const { data } = useSuspenseQuery(query.post(postId)) | ||
|
||
return ( | ||
<div> | ||
<h1>{data.title}</h1> | ||
<p>{data.body}</p> | ||
</div> | ||
) | ||
} | ||
``` | ||
|
||
```tsx /query.ts | ||
import { queryOptions } from '@suspensive/react-query' | ||
|
||
export const query = { | ||
post: (postId: number) => | ||
queryOptions({ | ||
queryKey: ['posts', postId], | ||
queryFn: async (): Promise<{ | ||
id: number | ||
title: string | ||
body: string | ||
}> => { | ||
const response = await fetch(`https://jsonplaceholder.typicode.com/posts/${postId}`) | ||
if (!response.ok) { | ||
throw new Error('An error occurred') | ||
} | ||
return await response.json() | ||
} | ||
}) | ||
} | ||
``` | ||
|
||
</Sandpack> |