TanStack Router
The adapter for TanStack Router is an entry point of its own, @qubeejs/react/tanstack-router. It
needs TanStack Router 1.49 or later, the first release whose navigate takes an href.
npm install @qubeejs/react @qubeejs/core @tanstack/react-routerWrap once
Section titled “Wrap once”import { TanStackRouterAdapter } from '@qubeejs/react/tanstack-router';import { createRootRoute, Outlet } from '@tanstack/react-router';
/** The root route: every list under it reads and writes the URL through TanStack Router. */export const rootRoute = createRootRoute({ component: () => ( <TanStackRouterAdapter> <Outlet /> </TanStackRouterAdapter> ),});Every list under the root route then goes through TanStack Router.
Keep the query as typed
Section titled “Keep the query as typed”TanStack Router owns the query string: it parses it into a search object and writes that object back on every navigation. Its default serialisers read each value as JSON, so the URL a list asks for is not always the URL it gets:
| The user types | TanStack Router writes |
|---|---|
10 (a space) |
10 |
"react hooks" |
react hooks |
1.50 |
1.5 |
1e3 |
1000 |
The list settles on what the router wrote — the URL always wins — so a search box loses those
characters when its debounce lands: 10 years, typed with a pause after the space, ends up as
10years.
If the app has a free-text search, give the router the two serialisers the entry ships, which keep text as text:
import { parseSearch, stringifySearch } from '@qubeejs/react/tanstack-router';import { createRoute, createRouter } from '@tanstack/react-router';
import { ArticlesPage } from './tanstack-router-articles';import { rootRoute } from './tanstack-router-root';
const articlesRoute = createRoute({ component: ArticlesPage, getParentRoute: () => rootRoute, path: '/articles',});
/** * The router. The two serialisers keep the query as plain text, so a search box * keeps what was typed. */export const router = createRouter({ parseSearch, routeTree: rootRoute.addChildren([articlesRoute]), stringifySearch,});parseSearch reads every value as the string it is in the
URL, and a repeated name as a list; stringifySearch
writes it back. With them, every search value in the app is a string, or a list of strings: a
route whose validateSearch wants a number has to coerce it. TanStack Router’s guide to
custom search param serialization
covers the alternatives.
A list page
Section titled “A list page”import { useQubeeList, useQubeeQuery } from '@qubeejs/react';import type { MouseEvent } from 'react';
import { type Article, articleList } from './article-list';import { isPlainClick } from './plain-click';
/** How long typing must pause before the URL changes. */const SEARCH_DEBOUNCE_MS = 300;
/** * The articles route. It names no router: the list takes its adapter from the * `<TanStackRouterAdapter>` at the root route. */export function ArticlesPage() { const list = useQubeeList(articleList); const articles = useQubeeQuery<Article>(list.request); const { page, q } = list.state; const lastPage = articles.data?.lastPage ?? 1;
/** A real link to a page, that navigates in place on a plain click. */ const pageLink = (target: number) => ({ href: list.href({ page: target }), onClick: (event: MouseEvent<HTMLAnchorElement>) => { if (!isPlainClick(event)) { return; }
event.preventDefault(); list.setPage(target); }, });
return ( <main aria-busy={list.isPending || articles.isFetching}> <input aria-label="Search articles" onChange={(event) => list.set({ q: event.target.value }, { debounce: SEARCH_DEBOUNCE_MS, replace: true }) } type="search" value={q ?? ''} /> <ul> {articles.data?.data.map((article) => ( <li key={article.id}>{article.title}</li> ))} </ul> <nav aria-label="Pagination"> {page > 1 && <a {...pageLink(page - 1)}>Previous</a>} {page < lastPage && <a {...pageLink(page + 1)}>Next</a>} </nav> </main> );}The links are real anchors with a plain-click handler, as in the
Pagination bar. Under a basepath, list.href(…) is the path inside
it — TanStack strips the basepath from the location’s pathname — so give the anchor
router.buildLocation({ href: list.href(…) }).publicHref, which adds the basepath back.
How it works
Section titled “How it works”- It reads
location.searchStr— the query string as TanStack Router serialised it — andlocation.pathname. - It navigates with
router.navigate({ href }), which takes a fully built href and applies the router’s basepath and rewrites, so the list’s URL rules — defaults left out, foreign parameters kept — apply unchanged. The router’s search serialisers still run on that href, as above. - A list navigation keeps the scroll position: TanStack Router would otherwise scroll to the top
on every sort, page or search.
<TanStackRouterAdapter scroll>— oruseTanStackRouterAdapter({ scroll: true })— hands the scrolling back to the router. - If the route declares
validateSearch, let it pass the list’s keys through untouched: a validator that drops unknown keys drops the list’s. - For the one list that should not follow the provider, pass
useTanStackRouterAdapter()to the hook — see Adapters & providers.