👀 Check out the changes in Suspensive v2. read more →
Documentation
@suspensive/react-query
infiniteQueryOptions

infiniteQueryOptions

infiniteQueryOptions helps utilize queryOptions in Infinite Query. This provides similar benefits to the ones offered by queryOptions (opens in a new tab).

import { infiniteQueryOptions, useSuspenseInfiniteQuery, SuspenseInfiniteQuery } from '@suspensive/react-query'
import { useInfiniteQuery, useQueryClient } from '@tanstack/react-query'
 
const postsInfiniteQueryOptions = (page) =>
  infiniteQueryOptions({
    queryKey: ['posts', page] as const,
    queryFn: ({
      queryKey: [, page], // You can use queryKey.
    }) => fetch(`https://example.com/posts?page=${page}`).then(res => res.json()),
    getNextPageParam: (lastPage) => lastPage.nextPage ?? false,
  })
 
// No need to create a custom query hook.
// You can directly utilize infiniteQueryOptions in useInfiniteQuery, useSuspenseInfiniteQuery, and SuspenseInfiniteQuery.
const postsQuery = useInfiniteQuery(postsInfiniteQueryOptions(1))
const { data: posts, fetchNextPage, hasNextPage } = useSuspenseInfiniteQuery({
  ...postsInfiniteQueryOptions(1),
  refetchInterval: 2000, // Extensibility is clearly expressed in usage.
})
const Example = () => (
  <SuspenseInfiniteQuery {...postsInfiniteQueryOptions(1)}>
    {({ data, fetchNextPage, hasNextPage }) => (
      <div>
        {data.pages.map((page, index) => (
          <div key={index}>
            {page.posts.map(post => (
              <div key={post.id}>{post.title}</div>
            ))}
          </div>
        ))}
        <button onClick={() => fetchNextPage()} disabled={!hasNextPage}>
          Load More
        </button>
      </div>
    )}
  </SuspenseInfiniteQuery>
)
 
// You can easily use queryKey and queryFn in queryClient's methods.
const queryClient = useQueryClient()
queryClient.refetchQueries(postsInfiniteQueryOptions(1))
queryClient.prefetchQuery(postsInfiniteQueryOptions(1))
queryClient.invalidateQueries(postsInfiniteQueryOptions(1))
queryClient.fetchQuery(postsInfiniteQueryOptions(1))
queryClient.resetQueries(postsInfiniteQueryOptions(1))
queryClient.cancelQueries(postsInfiniteQueryOptions(1))
import { Suspense } from '@suspensive/react'
import { infiniteQueryOptions, SuspenseInfiniteQuery } from '@suspensive/react-query'
import { useQueryClient } from '@tanstack/react-query'
import { getPosts } from './api'

const postsInfiniteQueryOptions = (page) =>
  infiniteQueryOptions({
    queryKey: ['posts', page],
    queryFn: ({ queryKey: [, page] }) => getPosts(page),
    getNextPageParam: (lastPage) => lastPage.nextPage ?? false,
  })

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

  return (
    <div>
      <button onClick={() => queryClient.resetQueries(postsInfiniteQueryOptions(1))}>Reset Query</button>
      <Suspense fallback={<div>Loading...</div>}>
        <SuspenseInfiniteQuery {...postsInfiniteQueryOptions(1)}>
          {({ data, fetchNextPage, hasNextPage }) => (
            <div>
              {data.pages.map((page, index) => (
                <div key={index}>
                  {page.posts.map(post => (
                    <div key={post.id}>
                      <h1>{post.title}</h1>
                      <p>{post.body}</p>
                    </div>
                  ))}
                </div>
              ))}
              <button onClick={() => fetchNextPage()} disabled={!hasNextPage}>
                Load More
              </button>
            </div>
          )}
        </SuspenseInfiniteQuery>
      </Suspense>
    </div>
  )
}