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}</>
})
수정된 날짜: