wrap
wrap์ @suspensive/react์์ ์ ๊ณตํ๋ Suspense, ErrorBoundary, ErrorBoundaryGroup ๋ฑ์ผ๋ก ์ปดํฌ๋ํธ๋ฅผ ๊ฐ์ธ๊ธฐ ์ํด ๋ง๋ค์ด์ก์ต๋๋ค.
import { wrap, ErrorBoundaryGroup } from '@suspensive/react'
import { useSuspenseQuery } from '@suspensive/react-query'
export const Page = wrap
.ErrorBoundaryGroup({ blockOutside: true })
.ErrorBoundary({
fallback: ({ error }) => <PageErrorFallback message={error.message} />,
})
.Suspense({ fallback: <PageSkeleton /> })
.on(
// <ErrorBoundaryGroup />, <ErrorBoundary />, <Suspense />์ ๊ฐ์ธ์ง <Page/> ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ญ๋๋ค.
() => {
const { data: postList } = useSuspenseQuery({
queryKey: ['posts'],
queryFn: () =>
fetch(`https://example.com/posts`).then((res) => res.json()),
})
return (
<>
<ErrorBoundaryGroup.Consumer>
{(group) => <button onClick={group.reset}>Reset all posts</button>}
</ErrorBoundaryGroup.Consumer>
{postList.map((post) => (
<PostItem id={post.id} />
))}
</>
)
}
)
const PostItem =
wrap
.ErrorBoundary({ fallback: ({ error }) => <>{error.message}</> })
.Suspense({ fallback: <PostSkeleton /> }).on <
{ id: string } >
// <ErrorBoundary/>, <Suspense/>์ ๊ฐ์ธ์ง <PostItem/> ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ญ๋๋ค.
((props) => {
const { data: post } = useSuspenseQuery({
queryKey: ['posts', props.id],
queryFn: () =>
fetch(`https://example.com/posts/${props.id}`).then((res) =>
res.json()
),
})
return <>{post.title}</>
})