Setup/React Router
React Router
The adapter for React Router is an entry point of its own, @qubeejs/react/react-router. It needs
React Router 7 or later — declarative, data or framework mode.
npm install @qubeejs/react @qubeejs/core react-routerWrap once
Section titled “Wrap once”import { ReactRouterAdapter } from '@qubeejs/react/react-router';import { createBrowserRouter, Outlet, RouterProvider } from 'react-router';
import { ArticlesPage } from './react-router-articles';
/** The root route: every list under it reads and writes the URL through React Router. */function Root() { return ( <ReactRouterAdapter> <Outlet /> </ReactRouterAdapter> );}
const router = createBrowserRouter([ { children: [{ element: <ArticlesPage />, path: 'articles' }], element: <Root />, path: '/', },]);
export function App() { return <RouterProvider router={router} />;}<ReactRouterAdapter> goes inside the router — in the root route’s component, around its
<Outlet /> — because it uses React Router’s own hooks. Every list under the root route then
reads and writes the URL through React Router.
A list page
Section titled “A list page”import { useQubeeList, useQubeeQuery } from '@qubeejs/react';import { Link } from 'react-router';
import { type Article, articleList } from './article-list';
/** 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 * `<ReactRouterAdapter>` at the root route, and its links are React Router's. */export function ArticlesPage() { const list = useQubeeList(articleList); const articles = useQubeeQuery<Article>(list.request); const { page, q } = list.state; const lastPage = articles.data?.lastPage ?? 1;
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 && <Link to={list.href({ page: page - 1 })}>Previous</Link>} {page < lastPage && <Link to={list.href({ page: page + 1 })}>Next</Link>} </nav> </main> );}<Link to={list.href(…)}> navigates in place and keeps a real href, so no click handler is needed.
How it works
Section titled “How it works”- It reads
useSearchParams()anduseLocation(), and navigates withuseNavigate(). React Router’snavigatemay return a promise; the list does not wait for it. - A list navigation keeps the scroll position: where the app renders
<ScrollRestoration />, React Router would otherwise scroll to the top on every sort, page or search.<ReactRouterAdapter scroll>— oruseReactRouterAdapter({ scroll: true })— hands the reset back to React Router. - Every component under the route reads the same URL, so each can call
useQubeeList(articleList). The draft andisPendingstay with the component that made a change — see Lists in the URL. - A route
loadercan fetch the page before the component renders, withfetchQubeePageor the TanStack Query options. - For the one list that should not follow the provider, pass
useReactRouterAdapter()as the second argument — see Adapters & providers. - On React Router 6, write the adapter yourself — the same three hooks, imported from
'react-router-dom'— and turn it into a provider withcreateAdapterProvider.