API Reference
The entire core public API — nine functions (five you'll use constantly, plus effect, boundary, configure, and onCleanup). The optional packages are indexed at the bottom.
import { html, mount, createState, computed, each, effect, boundary, configure, onCleanup } from "./src/index.js";html#
html(strings, ...values) → TemplateResultA tagged template. Write real HTML; interpolate values with ${}.
${() => x}— a live binding (text or attribute) that updates whenx's sources change.${value}— a static value, inserted once.onevent=${fn}— an event listener.- Returns an opaque
TemplateResult— pass it tomount, return it from a component, place it in another template, or use it ineach's render function.
Unsupported (throws a clear error): dynamic tag names (<${tag}>), dynamic/spread attribute names (<el ${x}>), interpolation inside <script>/<style>/<textarea>/<title> or HTML comments.
mount#
mount(component, target) → unmount()Renders a component (or a TemplateResult) into the DOM and starts it.
component— a function returninghtml, or aTemplateResultdirectly.target— a DOMElementor a CSS selector string.- Returns an
unmount()function that detaches the DOM and disposes all reactivity. See Cleanup.
createState#
createState(initialValue) → { get, set, peek }A reactive value. See State.
| Method | Description |
|---|---|
get() | Read; subscribes the current binding. |
set(next) | Write; updates dependents if the value changed (Object.is). |
peek() | Read without subscribing. |
computed#
computed(fn) → { get, peek }A lazy, cached, value-gated derived value. See Computed.
| Method | Description |
|---|---|
get() | Read; recomputes only if a dependency changed. |
peek() | Read without subscribing. |
Read-only — there is no set.
each#
each(items, keyFn, renderFn) → EachResultKeyed list rendering. Place the result in a template's child position. See Lists.
items—() => array(reactive) or a plainarray.keyFn—(item) => key— a stable, unique key per item.renderFn—(item) => TemplateResult— the template for one item.
configure#
configure({ dev }) → voidToggle development warnings. dev defaults to true. See Production mode.
onCleanup#
onCleanup(fn) → voidRegister a teardown function for the current component or list item. It runs when that component is unmounted or that list item is removed. Use it for timers, subscriptions, or third-party widgets you create during setup. See Cleanup.
const id = setInterval(tick, 1000);
onCleanup(() => clearInterval(id));Optional packages#
The core has no router, store, SSR, or sanitizer — those are optional packages you add only if you need them. Each is built on the core's public API and has its own page; this is the index of their entry exports.
| Package | Exports | Page |
|---|---|---|
@zoijs/router | createRouter(routes, options?) | Router |
@zoijs/resource | resource(fetcher, options?) | Resource |
@zoijs/action | action(fn) | Action |
@zoijs/head | title, description, meta | Head |
@zoijs/storage | storage(key, initial) | Storage |
@zoijs/forms | form(...) | Forms |
@zoijs/i18n | createI18n(...) | Internationalization |
@zoijs/ssr | renderToString, hydrate, serialize | Server Rendering |
@zoijs/sanitize | sanitize(dirty) → Node[] | Sanitize |
@zoijs/testing | render, screen, fireEvent, waitFor, cleanup, mockRouter | Testing |
@zoijs/devtools | createInspector, inspect | DevTools |
@zoijs/eslint-plugin | recommended, legacy-recommended configs | ESLint Plugin |
TypeScript#
Type definitions ship in src/index.d.ts; editors discover them automatically.
const count = createState(0); // State<number>
const name = createState<string>(""); // explicit
const full = computed(() => name.get()); // Computed<string>
interface Todo { id: number; text: string; done: boolean; }
const todos = createState<Todo[]>([]);
each(() => todos.get(), (t) => t.id, (t) => html`<li>${() => t.text}</li>`); // t: TodoType-check your code with tsc --noEmit — no framework build step. Run the framework's own type tests with npm run test:types.