Lists with an input
/projects/42/tasks?status=open shows the tasks of project 42, but 42 is in the path, not in the
query the list reads. A list declares what its request needs besides the URL as its input, by
annotating the third parameter of apply — see the core’s
Lists that need more than the URL ↗:
import { defineList, enumParam, integerParam, STRAPI_DRIVER } from '@qubeejs/core';
import { API_URL } from './article-list';
/** A task as the Strapi API returns it. */export type Task = { id: number; status: 'done' | 'open'; title: string;};
/** * The tasks of one project. The project is not in the query — it comes from the route path, * /projects/42/tasks — so the list declares it as its input, by annotating the third parameter * of `apply`. */export const taskList = defineList({ apply: (builder, { status }, { projectId }: { projectId: string }) => { builder.addFilter('project', projectId);
if (status) { builder.addFilter('status', status); } }, params: { page: integerParam('page', { default: 1, min: 1 }), status: enumParam('status', ['done', 'open'] as const), }, qubee: { baseUrl: API_URL, driver: STRAPI_DRIVER }, resource: 'tasks',});Passing the input
Section titled “Passing the input”useQubeeList takes the input after the list, and the adapter, if you pass one, after the input:
const tasks = useQubeeList(taskList, { projectId });const inMemory = useQubeeList(taskList, { projectId }, useMemoryAdapter());- It is required. Leaving it out is a compile error, and so is passing one to a list that
declares none. An input whose fields are all optional is passed as
{}. - It goes into
request, never into the URL.state,href(),set(),reset(),setPage()andtoggleSort()work as for any list; a new input changes the request at once, without navigating. - An equal input keeps the same request.
{ projectId }written in the render is a new object every time; while its values stay the same,list.request— and the handle — stay the same object, so they are safe in a dependency array. - Prefer an object, as the core does: adding a value later breaks no call site. A bare value
such as a
stringworks too.
Not ready yet: null
Section titled “Not ready yet: null”When the input is not known yet — a lookup still in flight, a route param missing — pass null.
The list then has no request: list.request is null, and the fetching hooks fetch nothing.
import { useQubeeList, useQubeeQuery } from '@qubeejs/react';
import { type Task, taskList } from './task-list';
type ProjectTasksProps = { /** The project's id once your lookup of its slug has answered; `undefined` until then. */ projectId: string | undefined;};
/** * The tasks of a project whose id is still being looked up. Until it is known, the input is * `null`: the list has no request, and nothing is fetched. */export function ProjectTasks({ projectId }: ProjectTasksProps) { const list = useQubeeList(taskList, projectId ? { projectId } : null); const tasks = useQubeeQuery<Task>(list.request);
if (list.request === null) { return <p role="status">Finding the project…</p>; }
return ( <ul aria-busy={list.isPending || tasks.isFetching}> {tasks.data?.data.map((task) => ( <li key={task.id}>{task.title}</li> ))} </ul> );}requestisListRequest | nullonly for a list that declares an input (QubeeListRequest): a list without one always has a request.useQubeeQueryanduseQubeeSWRshow no page while the request isnull—useQubeeSWRonly thefallbackDatayou gave it, if any — and none from before it while the next request loads.- With TanStack Query,
qubeeQueryOptions(null)skips the query, butplaceholderData: keepPreviousDatakeeps showing the last page.placeholderData: list.request ? keepPreviousData : undefinedhides it while the input isnull; TanStack still offers that page as the placeholder while the next request loads, so render the waiting state fromlist.request === null, as above, and checkisPlaceholderDatawhere an old page must not show. fetchQubeePagetakes a request only: check fornullbefore calling it.
The core does no I/O, so a lookup is yours: resolve the slug’s id however your app does, and pass
null until it has.
The adapter or the input?
Section titled “The adapter or the input?”With one argument after the list, the hook reads it as the adapter when it is an object whose
navigate is a function, and as the input otherwise. An input type that could hold a function at
navigate is therefore refused at compile time: the error says the input is not assignable to
never. Rename that field. A type that any object fits, such as object, is refused for the same
reason: give the input its fields.
Generic code
Section titled “Generic code”A component or hook that takes any list does not know whether the list needs an input. Typed with
QubeeListArgs, it forwards whatever its callers pass, and they stay
checked:
import type { ListDefinition, ListParams } from '@qubeejs/core';import type { QubeeListArgs, QubeeListHandle } from '@qubeejs/react';
function useTaskTable<T extends ListDefinition<ListParams>>( list: T, ...args: QubeeListArgs<T>): QubeeListHandle<T> { return useQubeeList(list, ...args);}Called with a list type it does not know yet, useQubeeList(list) compiles without an input: the
list reads as one without, as it does through ListDefinition<ListParams> in the core. Forward
QubeeListArgs so that callers cannot leave a required input out.