Skip to content
Setup/Plain React

Plain React

An app without a router library — a Vite app, a widget on a server-rendered page — keeps its lists in the URL through the browser’s own history. The adapter for that is in the main entry: nothing else to install.

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 <BrowserAdapter> reads location and writes with history.pushState and replaceState.

The page names no router. It calls the hook, and fetches what the hook asks for:

src/articles/articles-page.tsx
import { useQubeeList, useQubeeQuery } from '@qubeejs/react';
import { type Article, articleList, ArticleStatusEnum } from './article-list';
/** How long typing must pause before the URL changes. */
const SEARCH_DEBOUNCE_MS = 300;
/**
* Articles, searched, filtered, sorted and paged through the URL. The component names no router:
* the list takes its adapter from the provider at the root.
*/
export function ArticlesPage() {
const list = useQubeeList(articleList);
const articles = useQubeeQuery<Article>(list.request);
const { state } = list;
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={state.q ?? ''}
/>
<label>
<input
checked={state.status === ArticleStatusEnum.PUBLISHED}
onChange={(event) =>
list.set({ status: event.target.checked ? ArticleStatusEnum.PUBLISHED : undefined })
}
type="checkbox"
/>
Published only
</label>
<button onClick={() => list.toggleSort('title')} type="button">
Sort by title
</button>
{articles.error && <p role="alert">The articles could not be loaded.</p>}
<ul>
{articles.data?.data.map((article) => (
<li key={article.id}>{article.title}</li>
))}
</ul>
<button disabled={state.page <= 1} onClick={() => list.setPage(state.page - 1)} type="button">
Previous
</button>
<button
disabled={state.page >= lastPage}
onClick={() => list.setPage(state.page + 1)}
type="button"
>
Next
</button>
</main>
);
}

The Quick start walks through it line by line.

Without a router library a plain <a href> reloads the document. Keep the real href — it can be opened in a new tab, copied and crawled — and navigate in place on a plain click:

src/plain-click.ts
import type { MouseEvent } from 'react';
/**
* Whether a click on a link should navigate in place: the primary button, no
* modifier key. Anything else — a middle click, a ⌘-click — is left to the
* browser, which opens the href in a new tab.
*/
export function isPlainClick(event: MouseEvent<HTMLAnchorElement>): boolean {
return event.button === 0 && !event.altKey && !event.ctrlKey && !event.metaKey && !event.shiftKey;
}
src/articles/pagination-bar.tsx
import { getPageWindow } from '@qubeejs/core';
import type { QubeeListHandle } from '@qubeejs/react';
import type { MouseEvent } from 'react';
import type { articleList } from './article-list';
import { isPlainClick } from './plain-click';
type PaginationBarProps = {
/** The last page the API reported; 1 until the first page arrives. */
lastPage: number;
list: QubeeListHandle<typeof articleList>;
};
/** Previous, the page window with its gaps, and next — every page a real link. */
export function PaginationBar({ lastPage, list }: PaginationBarProps) {
const { page } = list.state;
/** Navigate in place on a plain click; leave any other click to the browser. */
const goTo = (target: number) => (event: MouseEvent<HTMLAnchorElement>) => {
if (!isPlainClick(event)) {
return;
}
event.preventDefault();
list.setPage(target);
};
return (
<nav aria-label="Pagination">
<ul>
<li>
{page > 1 ? (
<a aria-label="Previous page" href={list.href({ page: page - 1 })} onClick={goTo(page - 1)}>
‹
</a>
) : (
<span aria-hidden="true">‹</span>
)}
</li>
{getPageWindow(page, lastPage).map((item, index) => (
<li key={item === 'gap' ? `gap-${index}` : item}>
{item === 'gap' ? (
<span aria-hidden="true">…</span>
) : (
<a
aria-current={item === page ? 'page' : undefined}
aria-label={`Page ${item}`}
href={list.href({ page: item })}
onClick={goTo(item)}
>
{item}
</a>
)}
</li>
))}
<li>
{page < lastPage ? (
<a aria-label="Next page" href={list.href({ page: page + 1 })} onClick={goTo(page + 1)}>
›
</a>
) : (
<span aria-hidden="true">›</span>
)}
</li>
</ul>
</nav>
);
}
  • Every list stays in step. All the components under the provider, and any that call useBrowserAdapter() themselves, see the same URL — through the Back and Forward buttons too.
  • It observes its own navigations, and Back and Forward. A pushState by other code is not seen until the next popstate, so parameters that code added can be dropped by the next set(). When something else also writes the URL, that something is a router: use its adapter instead.
  • On the server it knows no URL. A list renders its defaults there, and its real state right after hydrating. To render the right page on the server, use a router that knows the URL there — see Server rendering.
  • For one list that should not follow the provider, pass useBrowserAdapter() as the second argument of useQubeeList — see Adapters & providers.
  • A router of your own — hash routing, say — becomes a provider with createAdapterProvider.