Skip to content
Guide/Adapters & providers

Adapters & providers

A list reads and writes its query through a router adapter: an object with pathname, search and navigate(href, { replace }) — the RouterAdapter type. useQubeeList finds its adapter in one of two ways.

Wrap the app in an adapter provider:

src/main.tsx
import { BrowserAdapter } from '@qubeejs/react';
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import { App } from './app';
const root = document.getElementById('root');
if (!root) {
throw new Error('index.html has no #root element');
}
// Every list below the provider reads and writes the URL through browser history.
createRoot(root).render(
<StrictMode>
<BrowserAdapter>
<App />
</BrowserAdapter>
</StrictMode>
);

Every list below it uses that adapter, and names no router:

src/articles/article-pager.tsx
import { useQubeeList } from '@qubeejs/react';
import { articleList } from './article-list';
/** Previous and next links for the articles list. It names no router: the provider supplies it. */
export function ArticlePager() {
const list = useQubeeList(articleList);
const { page } = list.state;
return (
<nav aria-label="Pagination">
{page > 1 && <a href={list.href({ page: page - 1 })}>Previous</a>}
<a href={list.href({ page: page + 1 })}>Next</a>
</nav>
);
}

<BrowserAdapter> is for apps without a router: it reads location and writes with history.pushState and replaceState. Inside React Router, TanStack Router or Next.js, use that router’s adapter instead — React Router, TanStack Router, Next.js — because their router would not see these navigations.

The three router adapters keep the scroll position when a list navigates: a filter or a page number that changes should not send the reader back to the top. Each takes scroll to hand that back to its router — <ReactRouterAdapter scroll>, or useNextAdapter({ scroll: true }) for one list.

The provider hands down the adapter hook, not a router value. Only the components that use a list re-render when the URL changes, never the whole subtree. The nearest provider wins, so a provider around part of the tree gives that part its own adapter.

const list = useQubeeList(articleList, useBrowserAdapter());

An adapter passed to the hook — after the list, or after its input — wins over the provider. Use it for the one list that must not follow the rest of the tree. The component still re-renders when the provider’s URL changes: hooks cannot be called conditionally, so the provider’s adapter is read either way.

With neither a provider nor an argument, useQubeeList throws MissingRouterAdapterError. It never falls back to browser history by itself: inside a router library, that would leave the router out of step with the URL.

For a router this package ships no adapter for, write a hook that returns a RouterAdapter and turn it into a provider:

src/routing/hash-adapter.ts
import { createAdapterProvider, type RouterAdapter } from '@qubeejs/react';
import { useMemo, useSyncExternalStore } from 'react';
/** Follow the URL hash. */
function subscribe(listener: () => void) {
window.addEventListener('hashchange', listener);
return () => window.removeEventListener('hashchange', listener);
}
/** The hash without its `#`: `/articles?page=2`. */
const readHash = () => window.location.hash.slice(1);
/** On the server there is no hash. */
const readServerHash = () => '';
/** A router adapter for an app that routes through the URL hash. */
function useHashAdapter(): RouterAdapter {
const hash = useSyncExternalStore(subscribe, readHash, readServerHash);
const [pathname = '', search = ''] = hash.split('?');
return useMemo(
(): RouterAdapter => ({
navigate: (href, { replace }) => {
if (replace) {
window.location.replace(`#${href}`);
} else {
window.location.hash = href;
}
},
pathname,
search,
}),
[pathname, search]
);
}
/** Wrap the app in it, and every list below reads and writes the hash. */
export const HashAdapter = createAdapterProvider(useHashAdapter);

createAdapterProvider takes a hook with no arguments. Call it once, at module level: a provider keeps the hook it was created with. After editing that hook, reload the page — Fast Refresh does not swap it.

Two different lists under one provider that name a URL parameter the same — both page, say — read and write the same value, so paging one pages the other. In development the hook logs a warning that names both lists and the parameter. Give the param another URL name in one of them:

page: integerParam('tagPage', { default: 1, min: 1 }),

When two lists should share a value — one search box driving two result lists — declare the param once and use the same object in both definitions. Lists that share a param object do not warn, and neither does one list used by several components.