Re-renders & snapshots
Every hook in the adapter reads an external store through React’s
useSyncExternalStore: the qubee store for
useQubee and useQubeeContext, the URL for useBrowserAdapter. That one choice explains
everything on this page.
One write, one snapshot
Section titled “One write, one snapshot”The store keeps a snapshot of its state and hands out the same object until the next write. React compares snapshots by identity, so a component re-renders after a write, and only then.
builder.addFilter('status', 'published'); // writes → a new snapshotbuilder.setLimit(50); // writes again → another snapshotWrites made together in an event handler re-render once: React 18 and 19 batch the updates. A chain
such as builder.setResource('articles').addSort('title', SortEnum.ASC).setLimit(20) in a click
handler is several writes (most query methods also reset the page) and one render.
Consistent reads
Section titled “Consistent reads”During a render, everything you read comes from the same snapshot: state,
builder.generateUri(), builder.hasNextPage(). There is no moment where the URI reflects a filter
that state does not — the tearing useSyncExternalStore exists to prevent in concurrent
rendering.
The snapshot is deeply frozen. Mutating it throws in strict mode; change the query through the builder instead.
Who re-renders
Section titled “Who re-renders”| Component | Re-renders when |
|---|---|
Calls useQubee() |
its own instance’s store is written |
Calls useQubeeContext() |
the store of the nearest provider’s instance is written |
Renders <QubeeProvider> |
never because of the query — the context holds the instance, not the state |
Calls useQubeeList() |
the URL changes, or its own draft does, on set() |
Calls useBrowserAdapter() |
its own navigate, another useBrowserAdapter instance’s navigate (they share one history fan-out), or Back/Forward — not when other code calls history.pushState |
A component that only renders children re-renders with its parent, as always; wrap expensive rows
in memo when a list is long.
The instance outlives its config
Section titled “The instance outlives its config”useQubee(config) and <QubeeProvider {...config}> create their instance once. A later render
with a different driver or baseUrl is ignored. To switch, give the component a new key, which
remounts it with a fresh instance:
<AuthorPicker key={apiUrl} onPick={setAuthor} />Server rendering
Section titled “Server rendering”useSyncExternalStore takes a third argument, the server snapshot, used while rendering on the
server and while hydrating.
- For
useQubeeand for<QubeeProvider>with a configuration, each tree gets a fresh instance, and the server snapshot is that instance’s initial state — the same on both sides, so a client component renders on the server and hydrates without a mismatch. - For
<QubeeProvider value={qubee}>, the instance is yours: create it per tree or per request, never as a module-level instance shared across server requests — and configure it the same way on both sides. A client-only write before hydration (fromlocalStorage, say) makes the client’s snapshot differ from the server HTML; make such writes in an effect. - For
useBrowserAdapter, the server knows no URL: its server snapshot is an empty path and search. A URL-driven list therefore renders its defaults on the server, then re-renders with the real URL as soon as it hydrates. When the server should render the right page — in the Next.js App Router — use the framework’s router, which knows the URL there. See Server rendering.