@daevsoft/jetz
v1.3.1
Published
A composable javascript framework
Readme
Build modern, reactive web interfaces using clean JavaScript composition — without JSX, build-time compilation flags, or complex template languages.
Inspired by the composable, declarative paradigm of Jetpack Compose, Jetz brings that elegance directly to web developers using native JavaScript functions and direct, lightweight DOM updates.
import { Jetz, stateOf } from "@daevsoft/jetz";
import { button, div, p } from "@daevsoft/jetz/ui";
const count = stateOf(0);
const App = () => div(
p("Count: ", count),
button({ onclick() { count.value++; } }, "Increment")
);
Jetz.mount(App, "#app");pnpm add @daevsoft/jetz
# or
npm install @daevsoft/jetzTable of Contents
- Why Jetz?
- Jetz in 60 Seconds
- Feature Overview
- Thinking in Jetz
- Jetz vs Traditional DOM
- Why No JSX or Templates?
- When Should I Use Jetz?
- Quick Start
- Learning Path
- Core Concepts
- 1. Elements & UI Composition
- 2. Components (Functions & Classes)
- 3. Reactive State (
stateOf,rememberOf) - 4. Computed State (
computed) - 5. Side-Effects (
effect) - 6. Reactive Collections (
listOf,sequenceOf) - 7. Keyed List Reconciliation (
loop) - 8. Conditional Rendering (
_if,_elseif,_else,ifElse) - 9. Component Lifecycle
- 10. Two-Way Data Binding (
bind) - 11. Reactive Listeners (
listen) - 12. DOM Utilities & Helper Methods
- 13. Deferred Tasks (
defer) - 14. Explicit Watchers (
watch,untrack)
- Scaling Large Data Sets
- From Small UI to Complete Application
- Application Features
- Jetz DevTools
- Real-World Example: Calculator
- Architecture Overview
- API Quick Reference
- Testing
- Dispatcher Pattern
- Architecture Overview
- Development & Build
- Publishing
- License & Community
Why Jetz?
Web interfaces frequently force developers to pick between two extremes:
- Low-level Imperative DOM APIs: Manual
createElement, verbose event listeners, and brittle DOM state synchronization. - Heavyweight Toolchains: Mandatory JSX compilers, virtual DOM diffing overhead, and specialized templating syntax.
Jetz offers a third way: declarative, composable JavaScript with reactive state management, without leaving standard JavaScript.
Traditional DOM Code:
create element → configure element → query element → append element → manually mutate element
Jetz Paradigm:
describe UI → compose components → declare reactive state → DOM updates automaticallyCore Philosophy
- UI = JavaScript Composition: Build DOM trees with simple, readable function calls (
div,button,p). - State = Reactive: State values notify bound elements directly; no virtual-DOM diffing passes required.
- Components = Composable: Package UI into pure functions or reusable classes with full lifecycle hooks.
- DOM = Direct and Lightweight: Clean abstraction over native elements that preserves direct element access when needed.
Jetz in 60 Seconds
The entire Jetz mental model comes down to four basic steps:
import { Jetz, stateOf } from "@daevsoft/jetz";
import { button, div, h1 } from "@daevsoft/jetz/ui";
// 1. Create reactive state
const count = stateOf(0);
// 2. Compose your UI tree
const App = () => div(
h1("Interactive Counter"),
// 3. React to state: pass state directly or update it on event
button({ onclick() {
count.value++; // automatically triggers fine-grained DOM update
} }, "Clicked ", count, " times")
);
// 4. Mount to your HTML document
Jetz.mount(App, "#app");No build step required to parse custom syntax. That is valid, executable JavaScript out of the box.
Feature Overview
| Feature | Built-in API | What It Solves |
|---|---|---|
| Declarative UI | div, span, button, inputText, ... | Compose HTML elements cleanly with nested function calls. |
| Components | Functions or extends Component | Reusable UI units with input parameters and private state. |
| Reactive State | stateOf(value) | Fine-grained single-value state with subscribers and watchers. |
| Remembered State | rememberOf(key, value) | State synchronized with localStorage across page reloads. |
| Derived State | computed(fn) | Auto-tracked computed values with zero manual dependency arrays. |
| Reactive Classes | css(fn) | Class attributes that re-evaluate whenever the state they read changes. |
| Side-Effects | effect(fn) | Auto-tracking effects with instant execution and disposal cleanup. |
| Explicit Watchers | watch(source, fn), untrack(fn) | Change-driven callbacks with old/new values, and reads that stay out of the dependency graph. |
| Reactive Lists | listOf(), sequenceOf() | Observable arrays with chainable methods (push, remove, sort). |
| Keyed Reconciliation | loop(list, keyFn, renderFn) | O(1) DOM element recycling and minimal mutations on array changes. |
| Shallow Row State | shallowStateOf(), rowOf() | Row-level reactivity for tables: one signal per record instead of one per cell. |
| Lazy Signals | lazy(fn) | Deferred initialization: initializer runs only on first .value read (like SolidJS). |
| Deferred Tasks | defer(taskFn, { loadingState }) | Run heavy mutations after the browser paints, with an optional loading indicator. |
| Auto Cleanup Lifecycle | loop(), components + disposeBindings() | Drops a row's or a component's subscriptions, computed values and listeners when its view leaves the DOM. |
| Conditional UI | _if, _elseif, _else, ifElse | Declarative, reactive conditional rendering without wrapper divs. |
| Component Lifecycle | onCreate, onMount, onUpdate, onDestroy | Deterministic setup and teardown for function and class components. |
| Two-Way Binding | { bind: state } | Instant two-way synchronization between input elements and state. |
| Dynamic Routing | Router, route, link, asLink | Client-side SPA routing with browser history and parameters. |
| Route Guards | Middleware, middleware | Async/sync navigation guards with reason-based denials. |
| Session State | JetzSession, sessionOf | Reactive key-value store automatically backed by sessionStorage. |
| Dispatcher | Dispatcher | Flux-like action dispatching for clean architecture. |
| DOM Utilities | find, findAll, .attr(), .addClass(), ... | Fluent helper methods on every element before and after mounting. |
| Safe Navigation | Jetz.allowUnsafeUrls | javascript: / vbscript: / data:text/html URLs are blocked in href, src, action, … |
| Error Reporting | Jetz.errorHandler, Jetz.reportError() | One central sink for the errors Jetz catches inside effects, watchers and lifecycle hooks. |
Thinking in Jetz
Adopting Jetz is a smooth shift from manual DOM plumbing to declarative composition:
HTML markup ───► JavaScript composition functions
Manual createElement / append ───► Nested function hierarchies
Manual DOM innerText updates ───► Reactive stateOf and computed values
Spaghetti event listeners ───► Inline declarative handler objects
Full list re-renders ───► Keyed loop reconciliation
Complex external build configs ───► Standard ES Modules and standard JSJetz vs Traditional DOM
Traditional Imperative DOM
// Verbose, error-prone, manual synchronization
const container = document.createElement("div");
container.className = "card";
const counterText = document.createElement("p");
let count = 0;
counterText.textContent = `Count: ${count}`;
const btn = document.createElement("button");
btn.textContent = "Increment";
btn.addEventListener("click", () => {
count++;
counterText.textContent = `Count: ${count}`; // Manual sync required
});
container.appendChild(counterText);
container.appendChild(btn);
document.body.appendChild(container);With Jetz
// Declarative, reactive, clean composition
import { Jetz, stateOf } from "jetz";
import { div, p, button, css } from "jetz/ui";
const count = stateOf(0);
const Card = div(css`card`,
p("Count: ", count),
button("Increment", {
onclick: () => count.value++
})
);
Jetz.mount(Card, document.body);The difference: In Jetz, the relationship between state and UI is declared once. When state changes, only the exact bound text node or attribute updates.
Why No JSX or Templates?
Many modern frameworks rely on JSX or custom template compilers (.vue, .svelte, .html). Jetz intentionally uses standard JavaScript functions:
- Zero Build Overhead for Syntax: You can run Jetz in modern browsers, prototyping tools, or standard ESM setups without requiring Babel, SWC, or TypeScript JSX transforms just to render an element.
- Full Power of JavaScript: Functions are just functions. Variables, loops, closures, conditionals, arrays, and standard language features work directly without templateDSL constraints.
- Transparent DOM Mapping:
div(...)creates aJetzElementthat wraps and produces real DOM elements directly. There are no hidden virtual DOM reconciliation layers getting between you and the browser. - Composable by Nature: Passing elements, components, or UI fragments as parameters, returning them from helpers, or composing them dynamically is as natural as writing regular JavaScript functions.
When Should I Use Jetz?
Great Fit For:
- Interactive Web Apps & SPAs: Full routing, state, session, and lifecycle built into a lightweight package.
- Dashboards & Internal Tools: Rapid prototyping and clean UI creation without heavy build tooling.
- Component-Driven Frontends: Teams and developers who prefer the clarity of functional composition over JSX.
- Performance-Sensitive Micro-UIs: Situations where virtual-DOM runtime overhead and large bundle sizes are unwanted.
When to Consider Alternatives:
- Projects where the engineering team is strictly mandated to use JSX/TSX syntax.
- Content-heavy static sites with zero interactivity (where plain static HTML/SSG is sufficient).
Quick Start
1. Installation
Install Jetz and the Rspack bundler tools:
# Using pnpm
pnpm add @daevsoft/jetz
pnpm add -D @rspack/core @rspack/cli
# Using npm
npm install @daevsoft/jetz
npm install -D @rspack/core @rspack/cli2. Minimal Setup Tutorial with Rspack
Here is a minimal, complete single-page application setup with Rspack and Jetz routing:
Directory Structure
my-jetz-app/
├── index.html
├── index.js
├── rspack.config.js
├── package.json
└── src/
├── app.js
└── home.jsA. Bundler Configuration (rspack.config.js)
import { rspack } from "@rspack/core";
export default {
entry: "./index.js",
plugins: [
new rspack.HtmlRspackPlugin({
template: "./index.html",
}),
],
devServer: {
hot: false,
},
};B. HTML Entry (index.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Jetz App</title>
</head>
<body>
<div id="app"></div>
</body>
</html>C. App Shell Component (src/app.js)
import { Jetz } from "@daevsoft/jetz";
import { main } from "@daevsoft/jetz/ui";
export const App = () => {
return main(
Jetz.$route.browser()
);
};D. Home Page View (src/home.js)
import { css, div } from "@daevsoft/jetz/ui";
export const Home = () => {
return div(css`text-gray-500`, "Hello World");
};E. Main Entry Point (index.js)
import { Jetz } from "@daevsoft/jetz";
import { route, Router } from "@daevsoft/jetz/router";
import { Home } from "./src/home.js";
import { App } from "./src/app.js";
const router = new Router([
route('/', Home)
]);
Jetz.use(router);
Jetz.mount(App, '#app');F. Run the Development Server
Add this script to your package.json:
{
"type": "module",
"scripts": {
"dev": "rspack serve",
"build": "rspack build"
}
}Then start the server:
npm run dev
# or
npx rspack serveLearning Path
Follow this structured guide to master Jetz step by step:
- Elements & UI Composition — Learn how HTML tags map to functions
- Components — Functional and class-based components
- Reactive State — State management with
stateOfandrememberOf - Computed State — Auto-tracked derived values with
computed - Side-Effects — Reactive watchers with
effect - Reactive Collections — Arrays with
listOfandsequenceOf - Keyed List Reconciliation — Fast list rendering with
loop - Conditional Rendering — Declarative branch switching
- Component Lifecycle —
onCreate,onMount,onUpdate,onDestroy - Two-Way Binding — Synchronizing form inputs
- Reactive Listeners — Dynamic styling & DOM reactions with
listen - DOM Utilities — Fluent element manipulation helpers
- Scaling Large Data Sets — Shallow rows and automatic cleanup for big tables
- Router & Middleware — Multi-page SPA navigation
- Session Storage — Tab-persistent state
- Explicit Watchers —
watchanduntrackfor change-driven callbacks
Core Concepts
1. Elements & UI Composition
Every HTML5 element is exported as a composable JavaScript function from jetz/ui:
import { div, h1, p, span, button, a, img, css } from "jetz/ui";
const Banner = div(
h1("Fast, Declarative UI"),
p("Composable JavaScript functions represent elements:"),
span("No templates. No JSX."),
button("Get Started", {
onclick: () => alert("Welcome!")
})
);Syntax Flexibility
Element functions accept arguments in any natural order:
- Strings & Numbers: Rendered as child text nodes.
- Child Elements: Appended directly into the parent.
- Objects: Configured as attributes, properties, or event handlers. Keys written
data_*/aria_*render hyphenated, so{ data_counter: n }writesdata-counter. - CSS Helpers: Tagged template
css\class-name`, a reactive functioncss(() => …), style objects, or a css block viastyle`…``. - Reactive States: Automatically bind their text content.
div(
css`card active`, // classes
{ id: "hero", role: "banner" }, // attributes
h2("Title"), // child element
"Text content" // text node
)Multiple css() calls on one element are merged, so reactive and static classes can be declared side by side. See Reactive Classes.
Reactive Tag Templates
Every element factory doubles as a tagged template. Interpolated functions are evaluated as reactive text and re-render whenever a state they read changes:
const stateOnline = stateOf(true);
div`Is Online : ${() => stateOnline.value ? "Yes" : "No"}`;
span`Hello ${username}!`; // states can be interpolated directlyComponents and static values keep working as children, so ``div`Status: ${Badge}``` renders the badge element.
⚠️ The same code written as a plain string — a normal
div(...)call with an untagged template literal — cannot work: JavaScript flattens the interpolated function to its source text and the closure is gone before jetz sees it. WithJetz.devtools = true, jetz warns when a text child looks like a stringified function and points at the tagged form. Thetexttag behaves the same way:
div(text`Count : ${() => count.value}`);The bundled stress page (public/stress.html) renders one tagged string per row — the Ticker column — and reports a dedicated Reactive string metric: a single shared write re-rendering every row's string under load.
2. Components (Functions & Classes)
You can define components in three ways: as variables, functions, or classes extending Component.
A. Function Components (Recommended)
Function components are simple JavaScript functions that return an element tree:
import { div, h3, p, css } from "jetz/ui";
export function UserCard(name, role) {
return div(css`user-card`,
h3(name),
p(role)
);
}
// Usage in parent:
const Page = div(
UserCard("Alice", "Frontend Engineer"),
UserCard("Bob", "Product Designer")
);Calling a function like UserCard(...) is an ordinary JavaScript call. It works well for simple element-building functions, but does not give that call its own lifecycle. To let Jetz invoke a child component, pass a function reference, such as div(UserCard). That form takes no arguments, so use a small wrapper when the child needs values:
import { stateOf } from "jetz";
import { div, inputText, p } from "jetz/ui";
function Greeting({ name }) {
return p("Hello, ", name);
}
const name = stateOf("Ada");
const GreetingForUser = () => Greeting({ name });
const Page = div(
inputText({ bind: name }),
GreetingForUser
);Jetz invokes GreetingForUser as the child component. The wrapper passes name into Greeting; because name is a State, the greeting updates when the input changes. Put lifecycle hooks in the function Jetz invokes (here, GreetingForUser), or use the function reference directly when it needs no parameters. Do not write div(Greeting(name.value)) if you need a child lifecycle: that calls Greeting immediately, outside Jetz's component invocation. Routed function components are different: the router passes route parameters to the function directly.
B. Class Components
For complex stateful components or object-oriented architectures, extend Component:
import { Component, stateOf } from "jetz";
import { div, button } from "jetz/ui";
export class CounterComponent extends Component {
count = stateOf(0);
increment() {
this.count.value++;
}
render() {
return div(
button("Clicked: ", this.count, {
onclick: () => this.increment() // use arrow function to preserve `this`
})
);
}
}
// Instantiate with `new` or `.new()`:
const App = div(
new CounterComponent(),
CounterComponent.new()
);3. Reactive State (stateOf, rememberOf)
State in Jetz is created using stateOf(initialValue).
import { stateOf } from "jetz";
import { div, button } from "jetz/ui";
const counter = stateOf(0);
// Reading & writing state
console.log(counter.value); // 0
counter.value = 10; // updates value & triggers UI re-renders
counter.setState(20); // same as counter.value = 20
// Manual subscriptions (if needed)
const unsubscribe = counter.subscribe((newValue, oldValue) => {
console.log(`Changed from ${oldValue} to ${newValue}`);
});Updating State Values
Assign to .value to update a reactive value; use setState() when you prefer an explicit setter. Both forms notify subscribers and update bound UI:
const count = stateOf(0);
count.value += 1;
count.setState(10);
const profile = stateOf({ name: "Ada" });
profile.name.value = "Grace"; // object properties are reactive states tooBatching State Updates
Use batch(fn) when one user action updates multiple related states. stateOf() holds the source values, computed() derives a value from them, and effect() reacts to that derived value. Without batch(), the effect below sees an intermediate name; with batch(), it runs once with the completed name.
import { batch, computed, effect, stateOf } from "jetz";
const firstName = stateOf("Ada");
const lastName = stateOf("Lovelace");
const fullName = computed(() => `${firstName.value} ${lastName.value}`);
effect(() => {
console.log(`Profile name: ${fullName.value}`);
});
// Without batch(), these writes would emit "Augusta Lovelace" then "Augusta King".
batch(() => {
firstName.value = "Augusta";
lastName.value = "King";
});
// The effect emits only "Augusta King" for the batched update.Values are readable immediately inside the callback. Computed values, effects, subscribers, and bound DOM update when the outermost batch finishes. Keep the callback synchronous.
Use stateOf for temporary UI or application state. Use a ListState for collections that need reactive add, remove, or replace operations.
Any value can be held, including falsy ones. null, false, 0, "", and undefined are all valid initial values, so a state can start out "empty" and be filled in later:
const filter = stateOf(""); // starts blank
const ready = stateOf(null); // no value yet
const chosen = stateOf(false);Reading Without Subscribing
A .value read inside computed() or effect() registers a dependency. When
you only want the current value, read it untracked:
const count = stateOf(0);
count.peek(); // current value, no dependency registered
count.getValue(); // raw stored value, without invoking getter trapsuntrack(() => …) does the same for a whole expression or a call that reads
several states — see Explicit Watchers.
For diagnostics, count.subscriberCount reports how many bindings still hang on
a state; a released view should leave none behind. Reading it retains nothing.
Persisting State with rememberOf
rememberOf(key, initialValue) works like stateOf, but synchronizes supported updates to localStorage and restores them on refresh. The key should remain stable between visits; remembered values are scoped to the current page path.
import { rememberOf } from "jetz";
const theme = rememberOf("theme", "light");
theme.value = "dark"; // automatically saved to localStorage
// Arrays can also be remembered:
const recentSearches = rememberOf("searches", []);
recentSearches.push("JavaScript"); // persistedFor remembered arrays, push(), set(), and clear() save automatically. Other list mutations still update the reactive UI, but do not currently write to storage. To persist a removal or other transformed result, replace the list with set():
recentSearches.set(recentSearches.values.filter(item => item !== "JavaScript"));Store JSON-serializable data in remembered arrays. If list items need reactive fields, save plain data and recreate the reactive item states when loading.
4. Computed State (computed)
computed(fn) creates derived state that automatically tracks its dependencies.
When any state accessed inside the computation function changes, the computed value re-evaluates automatically and updates all bound DOM elements:
import { stateOf, computed } from "jetz";
import { div, span, inputText } from "jetz/ui";
const firstName = stateOf("Ada");
const lastName = stateOf("Lovelace");
// Automatically tracks `firstName` and `lastName`
const fullName = computed(() => `${firstName.value} ${lastName.value}`);
const UserProfile = div(
span("Full Name: ", fullName), // Updates whenever firstName or lastName changes
inputText({ bind: firstName, placeholder: "First name" }),
inputText({ bind: lastName, placeholder: "Last name" })
);Chaining Computed States
Computed states are fully reactive states and can depend on other computed states:
const price = stateOf(100);
const qty = stateOf(2);
const subtotal = computed(() => price.value * qty.value);
const tax = computed(() => subtotal.value * 0.1);
const grandTotal = computed(() => subtotal.value + tax.value);Reactive Classes
css accepts either a tagged template or a plain function, and both can be reactive. Pick whichever reads better:
| Form | Use it when |
|---|---|
| css`base ${() => …}` | Only part of the class depends on state. |
| css(() => …) | The whole class is derived from state. |
In both cases the callback is evaluated as a computed value, and the element's class attribute is rewritten whenever the state it reads changes.
Use a callback interpolation when part of a class depends on state:
import { listOf, loop, stateOf } from "jetz";
import { css, li, ul } from "jetz/ui";
const tasks = listOf(
stateOf({ id: 201, done: true, title: "Sketch the onboarding flow" }),
stateOf({ id: 202, done: false, title: "Review the component API" })
);
const TaskList = ul(loop(tasks, task => li(
css`task-number ${() => task.done.value ? "completed" : ""}`,
task.title
)));The callback must read task.done.value; an expression like ${task.done ? "completed" : ""} is evaluated before css receives it and cannot track future changes. Static classes remain ordinary tagged templates, for example csstask-number completed``.
When the whole class depends on state, pass a function instead of a tagged template:
import { stateOf } from "jetz";
import { css, div } from "jetz/ui";
const count = stateOf(0);
const Counter = div(
css(() => count.value % 2 === 0 ? "text-red-200" : "text-green-200")
);Return null to drop the class entirely - handy for conditional styling that should leave no residue when inactive:
const ready = stateOf(false);
const Badge = div(css(() => ready.value ? "is-ready" : null));Several css() calls on the same element are merged, and every reactive part keeps updating. Static classes are preserved across recompositions:
const active = stateOf(true);
const Panel = div(
css("panel base"), // always present
css(() => active.value ? "is-active" : "is-idle") // swaps on change
);The same two forms work as a plain attribute, written either as class or as its className alias:
const count = stateOf(0);
const Heading = div({ class: () => (count.value ? "wow" : "now") }, "A counter");
const Same = div({ className: () => (count.value ? "wow" : "now") }, "A counter");className is the DOM property name, so it is accepted and mapped onto class before attributes are merged - it combines with any css() on the same element instead of overwriting it.
Only class and className reach the class attribute. Any other key reaches the DOM under the name you wrote - apart from the data_/aria_ underscore rule shown under Reactive Attributes - and HTML attribute names are case-insensitive, so div({ cssClass: () => … }) renders <div cssclass="now">. The callback does run, and keeps re-running - it updates an attribute that nothing styles, which is why the element looks inert. Jetz maps this one well-known alias and never guesses at the rest, so viewBox and preserveAspectRatio keep their casing and a mistyped key stays visible in the markup rather than being silently rerouted.
Two rules cover every form above:
- Read
.valueinside the callback. A callback tracks the states it reads, so() => count.value % 2updates while${count.value % 2 ? "a" : "b"}is evaluated beforecssever sees it and freezes at its first value. - Return
nullorfalseto contribute nothing. Those values are filtered out instead of being stringified, so no strayclass="false"is left behind.
Reactive Styles
The style helper accepts the same three forms as css: an object of properties, a tagged template holding a css declaration block, or a callback/State producing either.
import { stateOf } from "jetz";
import { div, style } from "jetz/ui";
// 1. object form
div(style({ color: "crimson", "max-width": "400px" }))
// 2. tagged template - a css block, straight in the markup
div(
style`
max-width: 400px;
margin: 30px auto;
padding: 20px;
border: 1px solid #e2e8f0;
border-radius: 8px;
font-family: sans-serif;
`,
"Card content"
)Interpolations inside the template stay reactive, exactly like css… and `div`…:
const accent = stateOf("#334155");
const Panel = div(
style`
color: ${() => accent.value};
padding: 20px;
`,
"Panel"
);
accent.value = "#dc2626"; // only the color declaration updatesA State (or callback) holding the whole block works too:
const theme = stateOf("background: #fff; color: #111;");
div(style(theme));The declaration block is parsed into individual properties, so each one keeps its own binding - a reactive interpolation re-renders just that property rather than rewriting the whole style attribute. Blank entries and malformed declarations are skipped, and null/false interpolations contribute nothing.
Reactive Attributes
Attribute helpers keep State values instead of converting them to strings early. You can also pass a callback to an attribute or style property; its .value reads are tracked and the DOM updates when they change:
import { stateOf } from "jetz";
import { a, aria_, data_, button, href, style } from "jetz/ui";
const destination = stateOf("/tasks");
const saving = stateOf(false);
const color = stateOf("crimson");
const Link = a(href(destination), "Tasks");
const SaveButton = button({ disabled: saving }, "Save");
const Status = a(
aria_({ busy: saving }),
data_({ destination }),
{ title: () => `Open ${destination.value}` },
"Status"
);
const Swatch = a(style({ color: () => color.value }), "Preview");HTML boolean attributes such as disabled are added for true and removed for false. aria-* and data-* values remain strings, so a false state becomes "false" rather than removing the attribute.
Underscores work for these two prefixes, which spares an object literal its quotes:
import { stateOf } from "jetz";
import { css, data_, div, p } from "jetz/ui";
const counter = stateOf(0);
const Row = p(css`counter-${counter}`, { data_counter: counter }, "Counter:", counter);
// renders data-counter="0" - [data-counter] and element.dataset.counter both find it
const Same = p(data_({ counter }), "Counter:", counter); // helper form, same attribute
const Note = div({ aria_labelledby: "total", data_row_index: 2 }); // aria-labelledby, data-row-indexEvery _ after a data or aria prefix becomes a -, so data_row_index reads back as dataset.rowIndex. No other key is rewritten - source_map and my_data stay exactly as written - and a key that merely starts with those letters (database_id) is left alone.
Two things to keep in mind:
- Write the tail lowercase. HTML lowercases attribute names, so
data_rowIndexlands asdata-rowindexanddataset.rowIndexmisses it;data_row_indexis the spelling that round-trips. - Pick one spelling per element.
{ data_counter: a }anddata_({ counter: b })name the same attribute, and a duplicated non-class attribute is merged into a list that onlyclassknows how to use - both values are dropped.
5. Side-Effects (effect)
effect(fn) runs an imperative side-effect function that automatically discovers its dependencies by intercepting .value reads.
It re-executes whenever any accessed state changes, and returns a dispose function:
import { stateOf, effect } from "jetz";
const counter = stateOf(0);
// Executes immediately, then re-runs on every counter change:
const dispose = effect(() => {
document.title = `Count: ${counter.value}`;
});
// Dynamic conditional tracking:
const loggingEnabled = stateOf(true);
const status = stateOf("idle");
effect(() => {
if (loggingEnabled.value) {
console.log("Current status:", status.value); // tracks status only when loggingEnabled is true
}
});
// Clean up when no longer needed:
dispose();Returning a Cleanup from an Effect
When the callback returns a function, effect() treats it as that run's
teardown: it executes before the next run and once more when the effect is
disposed. This is the React/Vue/Solid shape, and it is how an effect releases
what the previous run allocated - a timer, a subscription, an observer:
const pollEvery = stateOf(5000);
const stop = effect(() => {
const id = setInterval(loadFeed, pollEvery.value);
return () => clearInterval(id); // runs before the next pass, and on dispose
});
stop(); // last teardown still runsA cleanup that throws does not break the effect; the error is routed to
Jetz.errorHandler with source: "effect cleanup".
An
effect()created inside aloop()item is cleaned up for you: the reconciler calls itsdispose()when that row leaves the DOM. See Automatic Cleanup Lifecycle.
6. Reactive Collections (listOf, sequenceOf)
For dynamic arrays, Jetz provides reactive collections via listOf() and sequenceOf(). Use their mutation methods when the collection itself changes; they update rendered lists created with loop():
import { listOf, loop } from "jetz";
import { ul, li } from "jetz/ui";
const todos = listOf("Learn Jetz", "Build an App");
const TodoList = ul(loop(todos, todo => li(todo)));
todos.push("Test the app");
todos.remove("Build an App");
todos.set(["Ship the feature"]);Use push() to append, insertAt() to insert at a known position, remove() / removeAt() to delete, and set() to replace the full collection (for example, after filtering or loading new data). Use sequenceOf() when creating a list whose duplicate primitive values must remain distinct, or call asUnique() on an existing list.
Call asRemember(key) when an existing ListState should be restored from and saved to localStorage. Prefer an explicit, stable key so the list does not depend on creation order. Use rememberOf(key, initialValue) when you want to create a remembered value directly; use asRemember() when you already have a list to mark as remembered:
const selectedTags = listOf("news", "news").asUnique();
const recentSearches = listOf().asRemember("recent-searches");
recentSearches.push("Jetz"); // saved to localStorageComplete ListState API
| Method / Property | Description |
|---|---|
| list.push(...items) | Append one or more items and update rendered lists |
| list.set(newArray) | Replace all items; chainable |
| list.map(fn) | Return a new array of mapped values without changing the list |
| list.insertAt(index, ...items) | Insert items at an index |
| list.remove(item) | Remove the first matching item |
| list.removeAt(index) | Remove the item at an index |
| list.sort((a, b) => ...) | Sort items in place; chainable |
| list.replaceAt(index, item) | Swap one item and update rendered views |
| list.updateAt(index, fn) | Swap one item with fn(item, index); chainable |
| list.transform(fn) | Replace every item with fn(item, index) and re-render |
| list.filter(predicate) | Return a plain array without changing the list |
| list.find(predicate?) | Return an array of matching items (list.find(fn)[0] for one) |
| list.take(count) | Return up to the first count values as a plain array |
| list.at(index) / list.get(index) | Read one item by position |
| list.indexOf(item) / list.includes(item) | Position and membership lookups |
| list.clear() | Empty the collection |
| list.toState() | Convert plain object items into recursively reactive states |
| list.asUnique() | Make duplicate string/number values distinct entries; chainable |
| list.asRemember(key?) | Restore and persist the list using an optional stable key; chainable |
| list.size | Returns item count |
| list.values | Direct reference to underlying array |
| list.value | Tracked snapshot of the items; assigning an array replaces the list |
| list.version | The State behind that snapshot — what computed() and effect() subscribe to |
| list.subscribe(fn) / list.unsubscribe(fn) | Run fn after every mutation (it receives the version counter, so read list.value inside it) |
| list.first() / list.last() | Convenience accessors for boundary items |
ListState.map() follows the standard array behavior and returns a new array. Use transform() when you want to replace list items and update rendered views. Avoid changing list.values or nested item properties in place when you need a rendered update; use a ListState method or set() with the updated array. For a remembered list, use set() after mutations other than push(), set(), or clear() to save the result. asRemember() stores JSON-serialized values, so prefer plain serializable records over reactive State instances.
A list is readable, not only renderable: because .value is tracked, a computed() or effect() that reads it re-runs whenever the list changes, and a listOf() can be passed straight to a component as a collection prop.
7. Keyed List Reconciliation (loop)
When rendering large collections, full list re-rendering can be costly. Jetz provides keyed reconciliation through the 3-argument form of loop():
import { listOf, loop } from "jetz";
import { ul, li, button, div } from "jetz/ui";
const users = listOf(
{ id: 101, name: "Alice" },
{ id: 102, name: "Bob" }
);
const UserList = ul(
loop(
users,
user => user.id, // Key selector: unique identifier
user => li(user.name) // Render function
)
);Why Keys Matter
Without Keys (classic loop):
1000 items ──(1 item modified)──► Re-render all 1000 DOM elements
With Keys:
1000 items ──(1 item modified)──► Re-render ONLY the 1 modified DOM elementThe classic 2-argument form loop(list, renderFn) remains available for backward compatibility.
Keys must be unique. A key addresses exactly one row, so duplicate keys cannot be tracked 1:1. When a duplicate is detected the list warns once and falls back to the classic full re-render for that list, so every item keeps a visible row instead of one row silently winning the key.
Beyond diffing, loop() also owns the lifecycle of each item: every
effect(), computed(), subscription and listener created while that item
renders is released when the reconciler drops the view. For very large data sets
(pair loop() with shallow row state)
see Scaling Large Data Sets.
8. Conditional Rendering (_if, _elseif, _else, ifElse)
Jetz supports declarative conditional rendering without creating unnecessary wrapper elements.
A. Multi-Branch Conditionals (_if, _elseif, _else)
import { stateOf, _if, _elseif, _else } from "jetz";
import { div, button } from "jetz/ui";
const tab = stateOf("home");
const Content = div(
div(_if(() => tab.value === "home"), "Welcome to the Homepage"),
div(_elseif(() => tab.value === "profile"), "Your Profile Details"),
div(_else, "Page Not Found"),
button("Switch", {
onclick: () => tab.setState(tab.value === "home" ? "profile" : "home")
})
);B. Inline Two-Branch Conditionals (ifElse)
ifElse(conditionFn, trueBranch, falseBranch) evaluates inline and swaps content seamlessly in place:
import { stateOf, ifElse } from "jetz";
import { div, span, button } from "jetz/ui";
const isLoggedIn = stateOf(false);
const Nav = div(
ifElse(
() => isLoggedIn.value,
() => span("Welcome back!"),
() => button("Log In", { onclick: () => isLoggedIn.setState(true) })
)
);_show is an alias of _if — same behavior, same import — for code where
"show this block" reads better than "if this is true".
9. Component Lifecycle
Every Jetz component — whether function-based or class-based — supports four lifecycle stages:
Each component instance that Jetz invokes has its own lifecycle context, including nested and routed components. For a nested function component, pass its function reference to an element (for example, div(LiveClock)) and use the imported lifecycle hooks inside it. If it needs values, pass them through a wrapper function as shown in the Components section. A route such as route("/clock", LiveClock) can pass route parameters directly to the function. Class components keep using their onCreate(), onMount(), onUpdate(), and onDestroy() methods. Lifecycle methods are not called on function components; use the hook functions instead.
onCreate ──► onMount ──► onUpdate ──► onDestroy
(before DOM) (in DOM) (state delta) (removed)| Lifecycle Hook | Timing & Purpose |
|---|---|
| onCreate | Executes once before rendering occurs. Ideal for initializing local variables and preparing state. |
| onMount | Executes once immediately after the element is attached to the document. Safe to touch DOM, start timers, or fetch data. |
| onUpdate | Executes whenever a bound state changes while the component is active in the DOM. |
| onDestroy | Executes when the element is removed from DOM (e.g. route change, conditional removal, list delete, unmount). Ideal for clearing timers and subscriptions. |
Lifecycle in Function Components
import { onCreate, onMount, onUpdate, onDestroy, stateOf } from "jetz";
import { div } from "jetz/ui";
export function LiveClock() {
const time = stateOf(new Date().toLocaleTimeString());
let intervalId;
onCreate(() => console.log("Clock initializing..."));
onMount(() => {
intervalId = setInterval(() => {
time.setState(new Date().toLocaleTimeString());
}, 1000);
});
onUpdate(() => console.log("Clock updated to:", time.value));
onDestroy(() => {
clearInterval(intervalId);
console.log("Clock cleaned up.");
});
return div("Current Time: ", time);
}Lifecycle in Class Components
import { Component, stateOf } from "jetz";
import { div } from "jetz/ui";
export class LiveClockClass extends Component {
time = stateOf("");
intervalId = null;
onMount() {
this.intervalId = setInterval(() => {
this.time.value = new Date().toLocaleTimeString();
}, 1000);
}
onDestroy() {
clearInterval(this.intervalId);
}
render() {
return div("Time: ", this.time);
}
}10. Two-Way Data Binding (bind)
Bind any stateOf instance directly to an input element using the bind property:
import { stateOf } from "jetz";
import { div, inputText, p } from "jetz/ui";
const query = stateOf("");
const SearchBox = div(
inputText({
bind: query, // Two-way binding: updates query.value on user input
placeholder: "Search documentation..."
}),
p("Searching for: ", query)
);Whenever the user types, query.value updates immediately. Conversely, setting query.value = "something" updates the input field's display value automatically.
When a binding fails
A bind target has to be a reactive state. When it is not - most often because a
list item is missing the field its template binds - Jetz throws a
JetzBindingError naming the element, what it received, and the item and
template that have to be fixed, instead of an internal Cannot read properties of
undefined (reading 'value'):
Jetz: `bind` on <input type="checkbox"> expected a state but received undefined.
A `bind` target has to come from stateOf(), computed() or rememberOf().
`undefined` usually means the state object is missing that field, e.g.
stateOf({ id, title }) // then `bind: task.done` reads undefined
stateOf({ id, done: false, title }) // give every bound field a value
While rendering the list item at index 0 (key 201).
The item template binds:
task => li(input(type`checkbox`, { bind: task.done }))The error stays a TypeError, so existing try/catch blocks keep working while
the message tells you which line of your code to change.
Outside a loop() there is no item template to quote, so set Jetz.devtools = true
to also include the stack frame that built the element. It is off by default
because it captures a stack per element, which is measurable on large lists.
11. Reactive Listeners (listen)
listen(callback) registers an inline reactive effect tied directly to an element. It renders no DOM element of its own, executing once on mount and subsequently on every state change:
import { stateOf, listen } from "jetz";
import { div, button } from "jetz/ui";
const count = stateOf(0);
const Box = div(
"Current count: ", count,
// Automatically updates the parent div's CSS class as state changes:
listen(parent => {
parent.replaceClass(/count-\d+/, `count-${count.value}`);
}),
button("+1", { onclick: () => count.value++ })
);12. DOM Utilities & Helper Methods
Every JetzElement wraps an HTMLElement and provides a fluent chainable API that works both before and after mounting:
import { div, find, findAll } from "jetz/ui";
const box = div("Hello World", { "data-role": "card" });
// Attributes & Data
box.attr("data-role"); // Read attribute
box.addAttr("title", "Greetings"); // Set attribute
box.removeAttr("title"); // Remove attribute
box.data("role"); // Read data-* property
// Styling & Classes
box.setStyle({ color: "blue" }); // Apply inline styles
box.getStyle("color"); // Read style property
box.addClass("active"); // Add class
box.removeClass("active"); // Remove class
box.toggleClass("active"); // Toggle class
box.replaceClass("active", "idle");// Replace class
// DOM Tree & State
box.text("Updated Text"); // Replace text content
box.value(); // Read an input's value (pass a value to set it)
box.disable(); box.enable(); // Toggle disabled state
box.focus(); box.blur(); // Focus controls
box.append(child, ...); // Add children after mount
box.empty(); // Remove all children (and release their bindings)
box.remove(); // Remove element from DOM
box.getParent(); // Owning JetzElement, when there is one
// Events
box.on("click", handler); // addEventListener wrapper
box.off("click", handler); // removeEventListener wrapper
// Global Finders
const header = find("#main-header"); // Returns HTMLElement
const items = findAll(".list-item"); // Returns Array<HTMLElement>addClass() / removeClass() accept a whitespace-separated string or an array,
so a multiline class list is applied as its individual tokens.
replaceClass() matches a string or a RegExp.
13. Deferred Tasks (defer)
defer(taskFn, options?) runs a reactive mutation after the browser has had
a chance to paint. Without options it is a plain paint-aware deferral; with
{ loadingState } it also wraps the task in a loading indicator:
- Frame setup (sync) —
loadingState.value = trueruns immediately, so spinners and disabled buttons are drawn on the very next frame. - One frame + one macrotask later —
taskFnstarts only after that loading frame was painted, so a heavy DOM rebuild can no longer freeze the UI before the indicator ever appears. - Frame after the task — the signal returns to
falseonly once the task's own DOM changes finished painting, so the indicator cannot visually disappear while the UI is still busy.
import { stateOf, defer, ifElse } from "jetz";
import { button, div, span } from "jetz/ui";
const currentPage = stateOf(1);
const isNavigating = stateOf(false);
function handleNextPage() {
// one line: flips the signal, paints it, then advances the page
defer(() => currentPage.value++, { loadingState: isNavigating });
}
const page = div(
button({ onclick: handleNextPage, disabled: () => isNavigating.value }, "Next Page"),
// reactive indicator: visible only while defer() is working
ifElse(() => isNavigating.value,
() => span({ class: "spinner" }, " Loading..."),
() => null)
);Why requestAnimationFrame + setTimeout?
| Step | Timing | Purpose |
|---|---|---|
| loadingState.value = true | Immediate (sync) | Subscribers/DOM update right away; the browser paints them on the next frame. |
| rAF → setTimeout(0) | After frame 1's paint | The heavy taskFn starts only after the loading UI was actually drawn. |
| rAF(() => loadingState.value = false) | Frame after the task | Clears the flag only after the task's result painted — the indicator can't vanish while the UI is still frozen. |
Notes:
- Works without options too:
defer(taskFn)is a plain paint-aware deferral. - If
taskFnthrows, the indicator is still cleared (thefinallypath) and the error propagates. - Wrap
taskFninbatch(() => …)when it performs many signal writes and you want a single flush at the end.
14. Explicit Watchers (watch, untrack)
effect() re-runs on any state it read. watch() is the narrower tool: it
follows one declared source and hands the callback the new and old values —
Vue's watch in a few lines of JavaScript.
import { stateOf, watch } from "jetz";
const query = stateOf("");
const stop = watch(query, (value, oldValue) => {
console.log(`search: "${oldValue}" -> "${value}"`);
});
query.value = "jetz"; // logs: search: "" -> "jetz"
stop(); // watcher releasedOnly the source is tracked. States read inside the callback are not dependencies, so a watcher cannot widen its own trigger set by accident.
Sources
Pass a State, a getter, or an array of either:
const count = stateOf(0);
const label = stateOf("idle");
watch(count, (value) => document.title = String(value)); // one State
watch(() => `${label.value}: ${count.value}`, onChange); // computed expression
watch([count, label], ([c, l]) => log(c, l)); // several sourcesAn object created by stateOf({ … }) and a rowOf() record are not sources
on their own — their .value is not a single tracked signal. Watch a getter over
the field you care about instead:
const profile = stateOf({ name: "Ada" });
watch(() => profile.name.value, (name) => save(name)); // not watch(profile, …)
const row = rowOf({ id: 1, score: 10 });
watch(() => row.score, (score) => repaint(score));Options & cleanup
| Option | Effect |
|---|---|
| immediate: true | Run the callback once during setup, with oldValue as null. |
| once: true | Stop automatically after the first run. |
The third callback argument registers teardown for the next run — the same
contract as an effect() return value:
watch(socket, (url, previousUrl, onCleanup) => {
const connection = open(url);
onCleanup(() => connection.close()); // closes before the next run and on stop()
}, { immediate: true });Writes inside the callback are not re-tracked, and a callback that throws (or a
cleanup that throws) is reported through
Jetz.errorHandler rather than breaking the
signal that fired it. A watcher created inside a loop() item, an ifElse()
branch or a component is disposed with that view, exactly like effect() —
no manual stop() needed. Outside a managed render, keep the returned function.
Untracked reads (untrack)
untrack(fn) runs fn and returns its result, while every .value read inside
it is skipped by dependency tracking — Solid's untrack. Use it when a computed
or effect needs a peek at a value without subscribing to it:
import { effect, stateOf, untrack } from "jetz";
const page = stateOf(1);
const filter = stateOf("");
effect(() => {
// re-runs on page changes only; filter is read for context
report(page.value, untrack(() => filter.value));
});For a single State the same thing is state.peek(); untrack() covers a whole
expression, a loop over many states, or a function call you do not want to
inline. rawOf(row) is the shallow-row equivalent.
Scaling Large Data Sets
A grid holding tens of thousands of rows puts two separate pressures on the reactive engine: how much memory each record costs, and who releases a row once it leaves the screen. Jetz addresses both.
Shallow Row State (shallowStateOf, rowOf)
stateOf(object) walks the object and turns every property into its own
reactive State. For a 12-column row that is 12 State instances before you
even add computed() columns, and you pay for all 50 000 rows — including the
49 500 that are off-screen.
rowOf(object) (alias: shallowStateOf()) keeps the record as a plain object
and gives it one version signal:
import { computed, listOf, loop, rowOf } from "jetz";
import { table, tbody, tr, td, button } from "jetz/ui";
// One signal per row, not one per cell
const employees = listOf(
rowOf({ id: 1, name: "Alice", dept: "Engineering", score: 92 }),
rowOf({ id: 2, name: "Bob", dept: "Design", score: 78 })
);
const grid = table(
tbody(loop(employees, emp => emp.id, emp => tr(
td(emp.name), // immutable column: read once, free
td({ class: () => emp.dept }), // reactive: re-read on every bump
td(computed(() => emp.score)), // reactive: recomputes on every bump
td(button("+1", {
onclick: () => { emp.score = emp.score + 1; } // one write, one row refresh
}))
)))
);Result: clicking +1 sets emp.score = 93, which bumps that row's single
version signal. Only that one <tr> re-renders; the sibling row and the rest of
the grid are untouched, and no property-per-State object was ever allocated.
Which Forms Re-read the Row
A row bump only refreshes the bindings that tracked the row, so it matters how you read it:
| Form | Re-reads on row bump? | Use for |
|---|---|---|
| td(emp.score) | no | columns that never change |
| td(() => emp.score) | no — a bare function child is called once | function components |
| td(computed(() => emp.score)) | yes | mutable text cells |
| td({ "data-x": () => emp.score }) | yes | reactive attributes |
| td(css\cell ${() => emp.dept}`)| yes | reactive classes |
|td({ style: { width: () => emp.score + "px" } })` | yes | reactive styles |
This is the one rule to remember: a bare value or bare function child is evaluated once. Wrap mutable cell text in
computed()— or move it into an attribute,classorstyle— and the row bump reaches it. Immutable columns should stay plain, which is exactly where shallow rows save the most.
| Layout | Reactive objects per row (4 cells) | Heap for 50 000 rows |
|---|---|---|
| stateOf({ ... }) + computed() columns | 4 State + 4 computed per row | baseline |
| rowOf({ ... }) | 1 version State per row | ~7x lower |
Each shallow row carries four helpers:
| Member | Description |
|---|---|
| row.touch() | Bump the row version (row-level refresh) |
| row.set({ patch }) | Merge a patch and bump once |
| row.peek(key) | Read a property untracked, like rawOf() |
| row.toObject() | Plain, non-reactive copy of the record |
rowOf()falls back to plainstateOf()for primitives, arrays andJetzElements, so you can map it over mixed data without branching.
Lazy Signals (lazy)
lazy(initializer) creates a signal that defers its computation until the first
.value read. This mirrors SolidJS's lazy() and is useful for expensive
initialization that should only happen when actually needed.
import { lazy } from "jetz";
// Initializer runs only on first access
const heavyData = lazy(() => {
console.log("Computing..."); // Runs once
return expensiveComputation();
});
// First read triggers computation
console.log(heavyData.value); // Logs "Computing..." then returns result
// Subsequent reads return cached value (no re-computation)
console.log(heavyData.value); // Returns cached result directlyLazy Dataset Pattern
For large datasets that may not be needed immediately (e.g., a tab the user
never opens), wrap the data in lazy():
import { lazy, listOf, loop, rawOf, rowOf } from "jetz";
import { table, tbody, tr, td } from "jetz/ui";
// Dataset generated only when pipeline first runs
const allEmployees = lazy(() => fetchEmployees().map(rowOf));
const query = stateOf("");
const page = stateOf(1);
// Pipeline reads .value, triggering lazy init and tracking dependency
effect(() => {
const data = allEmployees.value; // lazy init happens here
const filtered = data.filter(emp =>
rawOf(emp).name.includes(query.value)
);
employees.set(paginate(filtered, page.value));
});Benefits:
- Startup performance — expensive data generation skipped until needed
- Memory — off-screen datasets never allocated if user doesn't visit
- Reactivity — works with
computed,effect, and DOM bindings like anyState - Write support —
lazyValue.value = newDatareplaces the cached value
Untracked Pipeline Reads (rawOf)
A tracked read inside a computed() or an effect() subscribes it. That is
what you want while rendering a cell, and exactly what you do not want in a
filter/sort pass that walks all 50 000 rows — one read per row would pin the
pipeline to the whole dataset.
rawOf(row) returns the underlying plain object, bypassing the row version
signal, so the read stays untracked:
import { computed, listOf, loop, rawOf, rowOf, stateOf } from "jetz";
import { table, tbody, tr, td } from "jetz/ui";
const all = listOf(...fetchEmployees().map(rowOf)); // the full dataset
const page = stateOf(1);
const query = stateOf("");
// rawOf() keeps these reads untracked: the pipeline is re-run from scratch on
// every keystroke, but the reactive graph never gains a dependency per row.
function visibleRows() {
return all
.filter(emp => rawOf(emp).name.toLowerCase().includes(query.value.toLowerCase()))
.sort((a, b) => rawOf(a).score - rawOf(b).score);
}
// `computed()` is happy with a primitive - the count of matches:
const matchCount = computed(() => visibleRows().length);
// For the rows themselves, drive the rendered list instead:
const employees = listOf(...visibleRows());
query.subscribe(() => employees.set(visibleRows()));
const grid = table(tbody(loop(employees, emp => emp.id, emp => tr(
td(emp.name), // immutable column
td(computed(() => emp.score)) // mutable column
))));Result: typing in the filter box re-runs visibleRows() over all 50 000
rows, yet the reactive graph still holds only two subscriptions (query and the
rendered page) instead of one per row per column. rawOf() is a no-op on values
that are not shallow rows, so it is safe to call on mixed data.
computed()should return a primitive.stateOf()walks an array/object argument and would rewrite the values handed to it, so derive a count (or any scalar) withcomputed()and push the list itself throughlist.set().
For a bulk edit, write the plain data first and bump once per row:
import { batch, rawOf, touchRow } from "jetz";
batch(() => {
for (const emp of employees.values) {
const data = rawOf(emp); // untracked write target
data.score += 1;
}
for (const emp of employees.values) {
touchRow(emp); // one refresh per row, coalesced by batch()
}
});Automatic Cleanup Lifecycle
A rendered view is not just DOM: it also owns effect() runs, computed()
dependency subscriptions, state subscriptions, style containers and event
listeners. If a row is removed while those stay subscribed, the state keeps the
whole detached <tr> subtree reachable — the classic "the grid scrolls for ten
minutes and the heap keeps climbing" leak.
Jetz opens a disposal scope for every managed render: a loop() item, an
ifElse() branch, and a component (class or function). Everything created while
that render runs registers its teardown there:
import { computed, effect, listOf, loop, rowOf } from "jetz";
import { table, tbody, tr, td } from "jetz/ui";
const employees = listOf(...fetchEmployees().map(rowOf));
const grid = table(tbody(loop(employees, emp => emp.id, emp => {
// created inside the item scope - disposed automatically with the row
effect(() => { trackRowInAnalytics(emp.name); });
return tr(
td({ class: () => emp.dept }),
td(computed(() => emp.score))
);
})));
// pagination / filter change -> rows are dropped
employees.set(nextPage);Result: employees.set(nextPage) releases, for every removed row:
| Released | How |
|---|---|
| effect() runs | the effect's own dispose() is called |
| computed() values | unsubscribed from all their dependencies |
| state subscriptions | attribute, class, bind, style and text bindings |
| event listeners | removeEventListener for every on* handler |
| state containers | text/element nodes and StyleState containers detached |
The scope is opened by loop() itself, so no extra code is needed for keyed
removal, item swap under the same key, a full classic refresh, or removeAt().
A component's scope is owned by the element it returns, so the effect() and
computed() a component creates are released together with its view — a
long-lived store keeps no subscription of a page that is already gone. A nested
component inherits the scope of the render that created it.
Nothing changes for a reactive resource created outside any managed render — in a module, an ev
