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.
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>;}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);How it works
Section titled “How it works”qubeeQueryOptions(request)returns what TanStack’s ownqueryOptions()returns, with two things set: thequeryKey—['qubee', request.uri, request.headers]— and aqueryFnthat callsfetchQubeePagewith TanStack’s abort signal. Everything else is yours to add beside it, asplaceholderDatais here.requestis built from the committed state, so the key changes once per navigation — never for a keystroke still inside its debounce.placeholderData: keepPreviousDatakeeps the current page on screen while the next one loads, so the table does not flash empty.- The data is typed from the options:
dataisPaginatedResult<Article>, and so isqueryClient.getQueryData(options.queryKey). - A status that is not
okis aQubeeFetchErrorinerror. list.isPendingcovers the debounce and the navigation,isFetchingthe request. Dim the table for either.
The rest of TanStack Query
Section titled “The rest of TanStack Query”The options fit wherever TanStack Query takes options:
// Suspenseconst { data } = useSuspenseQuery(qubeeQueryOptions<Article>(list.request));
// A loader, or a Server Component: prefetch, then hydrateawait queryClient.prefetchQuery(qubeeQueryOptions<Article>(request));
// Refetch every listawait queryClient.invalidateQueries({ queryKey: ['qubee'] });- A
nullrequest skips the query: thequeryFnis TanStack’sskipToken. Such options do not fituseSuspenseQuery, which cannot skip — the types say so. A list with an input has anullrequest while the input isnull;keepPreviousDatagoes on showing the last page then, so passplaceholderData: list.request ? keepPreviousData : undefinedand render a waiting state fromlist.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.