Skip to content
Guide/useQubee

useQubee

useQubee(config) gives a component its own qubee instance — a builder, a paginator and the store they share — and re-renders the component whenever that store changes. It is for lists whose query lives in memory: a picker in a dialog, a widget, an app without routing.

const { builder, paginator, store, state } = useQubee({
baseUrl: 'https://example.com/api',
driver: STRAPI_DRIVER,
});
Member What it is
builder The core’s QueryBuilder: every filter, sort, field and page method, unchanged.
paginator The core’s Paginator: paginate(body) parses a response and records its last page in the store.
store The QubeeStore both of them share.
state The store’s current snapshot, a QueryBuilderState — read it to render.

config is the core’s QubeeConfig: the driver, and optionally baseUrl, pagination, request and response. Every builder method, and what each driver makes of it, is documented with the core: QueryBuilder API ↗.

src/authors/author-picker.tsx
import { type RawResponse, SortEnum, STRAPI_DRIVER } from '@qubeejs/core';
import { useQubee } from '@qubeejs/react';
import { useEffect, useState } from 'react';
import { API_URL } from './article-list';
/** An author as the Strapi API returns it. */
type Author = {
id: number;
name: string;
};
type AuthorPickerProps = {
onPick: (author: Author) => void;
};
/** A paged list of authors in a dialog: its query lives in memory, not in the URL. */
export function AuthorPicker({ onPick }: AuthorPickerProps) {
const { builder, paginator, state, store } = useQubee({ baseUrl: API_URL, driver: STRAPI_DRIVER });
const [authors, setAuthors] = useState<Author[]>([]);
// Configure once, after mount. Strict Mode runs effects twice in development:
// the second run finds a resource already set, and leaves the query alone.
useEffect(() => {
if (store.getSnapshot().resource) {
return;
}
builder.setResource('authors').addSort('name', SortEnum.ASC).setLimit(10);
}, [builder, store]);
// Reading during render is safe: generateUri() derives from the current snapshot.
const uri = state.resource ? builder.generateUri() : null;
useEffect(() => {
if (!uri) {
return;
}
const controller = new AbortController();
fetch(uri, { signal: controller.signal })
.then((response) => response.json() as Promise<RawResponse>)
.then((body) => setAuthors(paginator.paginate<Author>(body).data))
.catch((error: unknown) => {
if (!controller.signal.aborted) {
console.error(error);
}
});
return () => controller.abort();
}, [paginator, uri]);
return (
<div>
<ul>
{authors.map((author) => (
<li key={author.id}>
<button onClick={() => onPick(author)} type="button">
{author.name}
</button>
</li>
))}
</ul>
<button disabled={builder.isFirstPage()} onClick={() => builder.previousPage()} type="button">
Previous
</button>
<button disabled={!builder.hasNextPage()} onClick={() => builder.nextPage()} type="button">
Next
</button>
</div>
);
}
  • The instance is created once, on the first render. Later changes to config are ignored; to switch drivers or base URLs, remount the component with a new key.
  • Write from events and effects; read anywhere. A builder method that changes the query writes to the store, and a write re-renders the component. Methods that only read — generateUri(), hasNextPage(), isFirstPage(), paginationHeaders() — are safe during render and always agree with state.
  • Configure once, even in Strict Mode. React runs effects twice in development. The picker’s first effect returns early when the store already has a resource, so the sort is not added twice.
  • paginate() tells the builder where the end is. Until a response has been parsed, the last page is unknown: hasNextPage() answers true and lastPage() throws.