Lists in the URL
useQubeeList(list, router) connects a list declared with the core’s defineList to the page URL.
It reads the URL into typed state, writes changes back through your router, and builds the request
for the current page. The URL is the only source of truth: a reload, a shared link and the back
button all land on the same list.
What it needs
Section titled “What it needs”- A list, declared once with
defineList— the driver, the resource, the URL parameters and how they become a query. The Quick start declaresarticleList; every param and the rules the URL follows are documented with the core: Lists & URL state ↗. - A router adapter: any object with
navigate(href, { replace }),pathnameandsearch. Set it once for the whole app with a provider such as<BrowserAdapter>, or pass one as the last argument:useBrowserAdapter(), or an adapter for React Router, TanStack Router or Next.js. Adapters & providers explains both. - Its input, when the list declares one: what its request needs besides the URL, such as a
project id from the route path —
useQubeeList(taskList, { projectId }), ornullwhile it is not known yet. Lists with an input explains it.
What it returns
Section titled “What it returns”| Member | Type | What it is for |
|---|---|---|
state |
ListState<typeof list> |
The list’s values: render from it, bind inputs to it. |
set(changes, options?) |
void |
Change any params at once. One call, at most one navigation. |
setPage(page, options?) |
void |
Shorthand for set({ page }). |
reset(options?) |
void |
Return every param to its default, in one navigation. |
toggleSort(field, options?) |
void |
There only when the list declares exactly one sortParam: sorts by the field, flipping the direction where the core’s rules say so. |
href(changes?) |
string |
The URL a change would lead to — for <a> and <Link>. |
request |
ListRequest |
{ uri, headers, paginate } for the page to fetch — null while the input of a list that declares one is null. |
isPending |
boolean |
A debounced change is waiting, or a navigation is under way. |
toggleSort follows the core’s rules. On its own it leaves exactly one sort: the field, flipped
when it was the primary sort, ascending otherwise — even if a secondary sort named it. With
{ multiple: true } it flips the field in place wherever it is, or appends it ascending, keeping
the others.
A complete page
Section titled “A complete page”import { useQubeeList, useQubeeQuery } from '@qubeejs/react';
import { type Article, articleList } from './article-list';import { ArticleSearch } from './article-search';import { ArticleTable } from './article-table';import { PaginationBar } from './pagination-bar';import { ResultRange } from './result-range';import { StatusChips } from './status-chips';
/** * The article list, whole. One useQubeeList serves every control, so the page * dims while the search box's change is pending, and while its page is fetched. */export function ArticleIndex() { const list = useQubeeList(articleList); const { data: result, isFetching } = useQubeeQuery<Article>(list.request);
return ( <section> <ArticleSearch list={list} /> <StatusChips list={list} /> <div aria-busy={list.isPending || isFetching}> <ArticleTable articles={result?.data ?? []} list={list} /> {result && <ResultRange result={result} />} </div> <PaginationBar lastPage={result?.lastPage ?? 1} list={list} /> </section> );}Its parts — the search box, the chips, the sortable headers, the pager — are explained in Search, debounce & pending state and Sorting & pagination UIs.
Links, not just handlers
Section titled “Links, not just handlers”Anything that navigates should also be a link, so it can be opened in a new tab, prefetched and
crawled. href(changes) returns the URL; a plain click can still change the list in place with
set:
import type { QubeeListHandle } from '@qubeejs/react';import type { MouseEvent } from 'react';
import { type articleList, ArticleStatusEnum } from './article-list';import { isPlainClick } from './plain-click';
/** The chips, in the order they are shown; `undefined` means every status. */const CHIPS = [ { label: 'All', status: undefined }, { label: 'Published', status: ArticleStatusEnum.PUBLISHED }, { label: 'Drafts', status: ArticleStatusEnum.DRAFT },] as const;
/** * Status filters as links: each is a real href — it opens in a new tab and can * be crawled — and a plain click changes the list in place. */export function StatusChips({ list }: { list: QubeeListHandle<typeof articleList> }) { return ( <nav aria-label="Status"> {CHIPS.map(({ label, status }) => ( <a aria-current={list.state.status === status ? 'true' : undefined} href={list.href({ status })} key={label} onClick={(event: MouseEvent<HTMLAnchorElement>) => { if (!isPlainClick(event)) { return; }
event.preventDefault(); list.set({ status }); }} > {label} </a> ))} </nav> );}To know whether a link is the current page — for an active style or aria-current — compare
list.href(changes) with list.href(). Both are canonical, so they agree whenever the two lists
are the same. Don’t compare raw query strings, even through toSearchParams(…).toString(): that
evens out the encoding, not the key order, so a hand-edited /articles?page=3&q=react never matches
the /articles?q=react&page=3 the list writes.
The rules the URL follows
Section titled “The rules the URL follows”They come from the core, and hold for every change, whichever member makes it:
- Defaults stay out of the URL. The first page, the default sort and an empty search leave no
trace: the unfiltered list is just
/articles. - A change goes back to page 1 when it changes anything but the page — the old page number means nothing in a different list. Setting a value to what it already is keeps the page.
- Parameters the list does not own survive. A
?panel=settingsput there by something else stays put. - A bad value falls back to its default. A hand-edited
?page=abcshows page 1; nothing throws.
One call, at most one navigation
Section titled “One call, at most one navigation”Clearing two filters is one history entry, and one request:
list.set({ q: undefined, status: undefined });Setting a key to undefined returns it to its default. For a param with an empty default, such as
q or status, that means no value; for a list or sort param with a non-empty default it means
that default, not nothing. Give a multi-select an empty default if users must be able to clear it.
list.reset() does the same for every param the list declares: one call, one history entry, and
parameters the list does not own stay where they are. On a list already at its defaults it does
nothing.
A change adds a history entry; pass { replace: true } to replace the current one instead — what a
search box wants, so that the back button does not step through every search.
Three layers of state
Section titled “Three layers of state”Between a keystroke and a fetched page, a change passes through three layers:
- Draft —
state. Updated the moment you callset(), so an input bound to it never lags behind a debounce or a slow server. - Committed —
request. Built from the last href handed tonavigate, after any debounce. A cache keyed onrequest.urichanges once per navigation, not once per keystroke. - URL — what the router reports. The source of truth: when it catches up with the committed href, the other two layers fall away.
A URL change the hook did not cause — the back button, a link elsewhere on the page — wins: the
draft and any waiting debounce are dropped, and state shows what the address says.
Routers that report the URL late
Section titled “Routers that report the URL late”Some routers report the new URL only after a transition — the Next.js App Router, for one. Two
edge cases follow from the gap between calling navigate and hearing the new URL back:
- A debounced change could fire after the user has started another navigation, but before the router reports it, and pull them back. So a click on a link while a debounce is waiting commits the change at once: the link the user follows always comes after it. What remains is a navigation started without a link — by a button’s handler, a timer, a keyboard shortcut of your own — while a debounce is waiting. A link that changes the same list in place, such as a page link with a click handler, is still a link: the waiting change is committed first and the link’s own change follows, in two navigations that end where one would have.
- An external navigation to a URL that is canonically equal to the current one, made while the
hook’s own navigation is in flight, can leave
state,requestandisPendingon the superseded navigation until the next change.
Both are rare, and both clear on the next change to the list.
One hook per list, or one per component
Section titled “One hook per list, or one per component”Every component that calls useQubeeList with the same list reads the same URL, so the state is
shared with nothing more than the adapter provider above them. What is not shared is the draft
and isPending: they belong to the hook that made the change. When a table should dim while the search box’s change is pending, call
the hook once in their common parent and pass the result down, as ArticleIndex does.