๐Ÿ‘€ Suspensive v2์—์„œ์˜ ๋ณ€๊ฒฝ์„ ํ™•์ธํ•˜์„ธ์š”. ๋”๋ณด๊ธฐ โ†’
๋ฌธ์„œ๋ณด๊ธฐ@suspensive/react-query<SuspenseInfiniteQuery/>

SuspenseInfiniteQuery

<SuspenseQuery/>๊ฐ€ useSuspenseQuery๋ฅผ jsx์—์„œ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•˜๊ฒŒ ํ•˜๋Š” ์—ญํ• ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ <SuspenseInfiniteQuery/>๋Š” useSuspenseInfiniteQuery๋ฅผ jsx์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์‰ฝ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•œ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.

import { SuspenseInfiniteQuery } from '@suspensive/react-query'
import { Suspense, ErrorBoundary } from '@suspensive/react'
import { PostListItem } from '~/components'
 
const InfinitePostsPage = ({ userId }) => (
  <ErrorBoundary fallback={({ error }) => <>{error.message}</>}>
    <Suspense fallback="loading...">
      <SuspenseInfiniteQuery {...postsInfiniteQueryOptions(userId)}>
        {({ data, fetchNextPage }) => (
          <>
            {data.pages.map((post) => (
              <PostListItem {...post} />
            ))}
            <button
              type="button"
              onClick={() => {
                fetchNextPage()
              }}
            >
              Load More
            </button>
          </>
        )}
      </SuspenseInfiniteQuery>
    </Suspense>
  </ErrorBoundary>
)
import React from 'react'
import { SuspenseInfiniteQuery } from '@suspensive/react-query'
import { Suspense, ErrorBoundary } from '@suspensive/react'
import { PostListItem } from './PostListItem'
import { postsInfiniteQueryOptions } from './queries'

export const Example = () => {
  return (
    <ErrorBoundary fallback={({ error }) => <>{error.message}</>}>
      <Suspense fallback={<div>Loading...</div>}>
        <div
          style={{
            display: 'flex',
            flexDirection: 'column',
            marginBottom: '50px',
          }}
        >
          <SuspenseInfiniteQuery
            {...postsInfiniteQueryOptions()}
            getNextPageParam={(lastPage, allPages) => {
              return lastPage.skip + lastPage.limit < lastPage.total
                ? allPages.length + 1
                : undefined
            }}
          >
            {({
              data: { pages },
              fetchNextPage,
              hasNextPage,
              isFetchingNextPage,
              isFetched,
            }) => (
              <>
                <ol>
                  {pages.map((page) =>
                    page.data.map((post) => (
                      <PostListItem key={post.id} {...post} />
                    ))
                  )}
                </ol>
                <button
                  onClick={() => fetchNextPage()}
                  disabled={!hasNextPage || isFetchingNextPage}
                >
                  {isFetchingNextPage
                    ? 'Loading more...'
                    : hasNextPage
                      ? 'Load More'
                      : 'Nothing more to load'}
                </button>
              </>
            )}
          </SuspenseInfiniteQuery>
        </div>
      </Suspense>
    </ErrorBoundary>
  )
}