Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(suspensive.org): add sandpack examples to queryOptions #1010

Merged
merged 1 commit into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Sandpack } from '@/components'

# queryOptions

Tkdodo, The maintainer of TanStack Query explains well why this interface is needed in [video explaining queryOptions in TanStack Query v5](https://youtu.be/bhE3wuB_TuA?feature=shared&t=1697).
Expand Down Expand Up @@ -44,3 +46,63 @@ queryClient.resetQueries(postQueryOptions(1))
queryClient.cancelQueries(postQueryOptions(1))

```

<Sandpack>

```tsx Example.tsx active
import { Suspense } from '@suspensive/react'
import { queryOptions, SuspenseQuery } from '@suspensive/react-query'
import { useQueryClient } from '@tanstack/react-query'
import { getPost } from './api'

const postQueryOptions = (postId: number) =>
queryOptions({
queryKey: ['posts', postId],
queryFn: () => getPost(postId),
})

export const Example = () => {
const queryClient = useQueryClient()

return (
<div>
<button onClick={() => queryClient.resetQueries(postQueryOptions(1))}>Reset Query</button>
<Suspense fallback={<div>Loading...</div>}>
<SuspenseQuery {...postQueryOptions(1)}>
{({ data }) => {
return (
<div>
<h1>{data.title}</h1>
<p>{data.body}</p>
</div>
)
}}
</SuspenseQuery>
</Suspense>
</div>
)
}
```

```tsx api.ts
type Post = {
userId: number
id: number
title: string
body: string
}

export const getPost = async (id: number): Promise<Post> => {
const response = await fetch(`https://jsonplaceholder.typicode.com/posts/${id}`)

if (!response.ok) {
throw new Error('An error occurred')
}

const data = await response.json()

return data
}
```

</Sandpack>
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Sandpack } from '@/components'

# queryOptions

TanStack Query의 메인테이너 [Tkdodo의 TanStack Query v5의 queryOptions 설명 영상](https://youtu.be/bhE3wuB_TuA?feature=shared&t=1697)에서 이 interface가 필요한 이유를 잘 설명되어 있습니다.
Expand Down Expand Up @@ -44,3 +46,63 @@ queryClient.resetQueries(postQueryOptions(1))
queryClient.cancelQueries(postQueryOptions(1))

```

<Sandpack>

```tsx Example.tsx active
import { Suspense } from '@suspensive/react'
import { queryOptions, SuspenseQuery } from '@suspensive/react-query'
import { useQueryClient } from '@tanstack/react-query'
import { getPost } from './api'

const postQueryOptions = (postId: number) =>
queryOptions({
queryKey: ['posts', postId],
queryFn: () => getPost(postId),
})

export const Example = () => {
const queryClient = useQueryClient()

return (
<div>
<button onClick={() => queryClient.resetQueries(postQueryOptions(1))}>Reset Query</button>
<Suspense fallback={<div>Loading...</div>}>
<SuspenseQuery {...postQueryOptions(1)}>
{({ data }) => {
return (
<div>
<h1>{data.title}</h1>
<p>{data.body}</p>
</div>
)
}}
</SuspenseQuery>
</Suspense>
</div>
)
}
```

```tsx api.ts
type Post = {
userId: number
id: number
title: string
body: string
}

export const getPost = async (id: number): Promise<Post> => {
const response = await fetch(`https://jsonplaceholder.typicode.com/posts/${id}`)

if (!response.ok) {
throw new Error('An error occurred')
}

const data = await response.json()

return data
}
```

</Sandpack>
Loading