Skip to content
Recipes/TanStack Query

TanStack Query

The entry point @qubeejs/react/tanstack-query turns a list’s request into TanStack Query options. It needs @tanstack/react-query 5.62 or later.

src/query-root.tsx
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { type ReactNode, useState } from 'react';
/** One QueryClient for the app, created once. */
export function QueryRoot({ children }: { children: ReactNode }) {
const [client] = useState(() => new QueryClient());
return <QueryClientProvider client={client}>{children}</QueryClientProvider>;
}
src/articles/use-articles-query.ts
import type { QubeeListHandle } from '@qubeejs/react';
import { qubeeQueryOptions } from '@qubeejs/react/tanstack-query';
import { keepPreviousData, useQuery } from '@tanstack/react-query';
import type { Article, articleList } from './article-list';
/**
* The page the list's committed state asks for, cached by TanStack Query. The
* key changes once per navigation — not per keystroke — because `request` is
* built from the committed layer, after any debounce.
*/
export function useArticlesQuery(list: QubeeListHandle<typeof articleList>) {
return useQuery({
...qubeeQueryOptions<Article>(list.request),
placeholderData: keepPreviousData,
});
}

In a component under QueryRoot:

const list = useQubeeList(articleList);
const { data, isFetching } = useArticlesQuery(list);
  • qubeeQueryOptions(request) returns what TanStack’s own queryOptions() returns, with two things set: the queryKey — ['qubee', request.uri, request.headers] — and a queryFn that calls fetchQubeePage with TanStack’s abort signal. Everything else is yours to add beside it, as placeholderData is here.
  • request is built from the committed state, so the key changes once per navigation — never for a keystroke still inside its debounce.
  • placeholderData: keepPreviousData keeps the current page on screen while the next one loads, so the table does not flash empty.
  • The data is typed from the options: data is PaginatedResult<Article>, and so is queryClient.getQueryData(options.queryKey).
  • A status that is not ok is a QubeeFetchError in error.
  • list.isPending covers the debounce and the navigation, isFetching the request. Dim the table for either.

The options fit wherever TanStack Query takes options:

// Suspense
const { data } = useSuspenseQuery(qubeeQueryOptions<Article>(list.request));
// A loader, or a Server Component: prefetch, then hydrate
await queryClient.prefetchQuery(qubeeQueryOptions<Article>(request));
// Refetch every list
await queryClient.invalidateQueries({ queryKey: ['qubee'] });
  • A null request skips the query: the queryFn is TanStack’s skipToken. Such options do not fit useSuspenseQuery, which cannot skip — the types say so. A list with an input has a null request while the input is null; keepPreviousData goes on showing the last page then, so pass placeholderData: list.request ? keepPreviousData : undefined and render a waiting state from list.request === null.
  • The fetcher is an argument: qubeeQueryOptions(request, { fetcher: authFetch }). The function also runs outside React, so it does not read <QubeeFetchProvider>. It is not part of the key: queries for the same request share one cache entry, whatever their fetchers.
  • It is not a client module, so a Server Component can build the options to prefetch a page.