Fetching data from a remote API or database is a core task for most applications.
Cerberus Signals provide foundational primitives like createQuery
and createMutation to manage asynchronous data.
Cerberus Signals data fetching APIs come with the following benefits:
To fetch (and cache) data using signals, simply follow two steps:
createQueryuseQuery in your componentIn this example we use a Signal to trigger a new query request. When using this design you, opt-out of optimistic UI updates and fallback to legacy loading-based UI changes.
In this example there are a few things happening:
currentUser stateWhen you pass an Signal Accessor into the query definition, it will auto-fetch, invalidate, and cache the result when the signal Accessor updates. This means, with this design mutations are not neccessary - but still strongly recommended.
When you want to perform an action related to a query, you utilize a mutation factory via createMutation.
When combined with query.key, this factory will automagically sync and update the query if it is listed in the invalidate options.
Even more, when combined with onMutate/onSetData, the UI will optimistically update while the query runs in the background creating a "real-time" like experience in the UI.
Here's what's happening in this demo:
onMutate/setQueryData provides optimistic updates to the query
b. invalidate breaks the query cache to ensure the latest data is fetched in the backgroundmutate helper to trigger mutations.In an SSR environment, you execute the factory's raw fetcher directly, bypassing the reactive cache. Then, you pass that data down to your Client Components to "hydrate" or seed the Cerberus cache, ensuring the client doesn't double-fetch on mount.
This is the standard SSR pattern for React.
Expose the raw, stateless fetcher function from your factory. You simply await it like a standard asynchronous function.
On the client side, use the initialData property. If the Cerberus cache is empty, it will instantly seed the cache with the server's data, skipping the <Suspense> boundary entirely.
Cerberus queries also support streaming data via Async Generators. This is powerful if you are using an LLM API or your local API supports data streaming.
{
"id": "52c4be29-50dc-4574-afe5-aa3b5fd9f21a",
"name": "User 52c4be29-50dc-4574-afe5-aa3b5fd9f21a"
}User f5e34db0-9971-4e32-85c9-67627a838b4b