Signals and effects
State and derived values
Section titled “State and derived values”Use signal(initial) for state that can change. Read or assign its .value. Writes are compared with Object.is, so an object or array stored in a signal is observed at the value boundary. Assign a new value when changing its structure. Use a separate signal for a field that should update independently.
import { computed, signal } from "min-reactive-framework";
const items = signal<readonly string[]>(["A"]);const count = computed(() => items.value.length);
items.value = [...items.value, "B"];console.log(count.value); // 2computed is read-only, runs on demand, and remembers the result until a dependency changes. Its dependencies come from the reads of its most recent evaluation. Keep the calculation pure. Use an effect for subscriptions or other external work.
Live bindings and snapshots
Section titled “Live bindings and snapshots”| Expression | Behavior |
|---|---|
p(value) when value is a signal |
Subscribes the text node to the signal. |
p(() => value.value.toUpperCase()) |
Tracks reads performed by the text expression. |
p(value.value) |
Captures the string at component setup time. |
Each binding has its own dependencies. The component function runs once per mount and the binding changes the existing text node when its result changes.
Batch writes
Section titled “Batch writes”Inside batch, you can read the latest signal and computed values. DOM work and effects run when the outermost synchronous batch exits. Try the button below. Two writes update the result and the effect runs once for the click.
Loading the example…
View source
import { batch, computed, effect, mount, signal } from "min-reactive-framework";import type { Dispose } from "min-reactive-framework";import { button, div, output, p } from "min-reactive-framework/html";import type { DemoLanguage } from "../example-language.js";
export function run(target: HTMLElement, lang: DemoLanguage): Dispose { const ru = lang === "ru"; const count = signal(0); const doubled = computed(() => count.value * 2); const runs = signal(0); let totalRuns = 0;
return mount(target, () => { effect(() => { void doubled.value; runs.value = ++totalRuns; });
return div( { class: "mr-demo-stack" }, button( { type: "button", on: { click: () => batch(() => { count.value++; count.value++; }), }, }, ru ? "Добавить 2 через batch" : "Add 2 with batch", ), p(ru ? "Сигнал " : "Signal ", output({ id: "signal-value" }, count)), p( ru ? "Вычисленное значение " : "Computed value ", output({ id: "computed-value" }, doubled), ), p(ru ? "Запуски эффекта " : "Effect runs ", output({ id: "effect-runs" }, runs)), ); });}batch tracks reads made inside it. Its scope ends on synchronous return, so code after await runs in a new synchronous turn.
Effects and cleanup
Section titled “Effects and cleanup”effect observes reads in its callback and reruns when the observed values change. Return a cleanup function to release resources before the next run or when the effect is disposed. An effect created during mounting belongs to that owner. A standalone effect returns a disposer that you can call directly.
import { effect, signal, untracked } from "min-reactive-framework";
const selected = signal("blue");const debug = signal(false);
const stop = effect(() => { const color = selected.value; if (untracked(() => debug.value)) console.info(color);});
selected.value = "green";stop();untracked permits an incidental read without adding a dependency. When an effect reads and writes the same signal, use this distinction deliberately. A chain of writes that keeps scheduling itself is reported as a reactive cycle.
See the reactivity signatures for scheduling and error behavior.