@zoijs/resource
The simplest way to read async data. A resource wraps a fetcher and gives you reactive loading, data, and error states.
npm install @zoijs/core @zoijs/resourceExample#
import { html, mount } from "@zoijs/core";
import { resource } from "@zoijs/resource";
function Profile() {
const user = resource(() => fetch("/api/user").then((r) => r.json()));
return html`
${() =>
user.loading() ? html`<p>Loading…</p>`
: user.error() ? html`<p>Couldn't load.</p>`
: html`<h1>${user.data().name}</h1>`}
`;
}
mount(Profile, "#app");It runs the fetcher once on creation. The three readers are reactive — read them inside a binding and the DOM updates when the data arrives.
API#
| Member | What it gives you |
|---|---|
resource(fetcher) | Create a resource; loads immediately |
data() | The loaded value, or undefined before the first success |
loading() | true while a load is in flight |
error() | The error, or null |
refresh() | Load again (keeps old data until the new load resolves) |
Pairs with @zoijs/action for writes: resource reads, action writes.
Known limitations#
- No cache, request dedupe, or retries — each
resource(...)is its own state. - Client-side only; no SSR/serialization.