Skip to content

Rendering and lifetime API

Import these APIs from min-reactive-framework.

function mount(target: Element | ShadowRoot, render: Children): Dispose;

Mounts the children returned by render as an owned range. Several mounts can share a target. The idempotent disposer removes its own range and stops its resources. Call it before replacing or removing a mounted region through external code. Initial mount failures release the resources created during that mount.

function onMount(run: () => void): void;
function onCleanup(dispose: Dispose): void;

Register both in an active lifetime. onMount runs after insertion and bindings are applied. onCleanup runs when the lifetime ends. In a user effect, cleanup belongs to its current run. Cleanup continues for other resources if one callback throws.

function when(condition: ReactiveValue<boolean>, yes: Children, no?: Children): DocumentFragment;

Mounts the factory for the current boolean result. A missing no leaves an empty region for false. Switching the result creates the next branch and disposes the previous one. If the next factory throws during setup, its resources are cleaned up and the previous branch stays mounted.

type Key = string | number;
interface ListOptions {
readonly empty?: Children;
}
function list<T>(
items: ReactiveValue<readonly T[]>,
key: (item: T) => Key,
render: (item: ReadonlySignal<T>, index: ReadonlySignal<number>) => Child,
options?: ListOptions,
): DocumentFragment;

Reconciles an array by unique keys. Each row has its own lifetime. Use item.value and index.value inside bindings to respond to changes in data or position. Invalid or duplicate keys are reported before the DOM update. The optional empty factory is mounted when the array is empty.

type TextValue = string | number | bigint | boolean | null | undefined;
type TextChild = ReactiveValue<TextValue>;
type Child = Element | Text | Comment | DocumentFragment | TextChild | readonly Child[];
type Children = () => Child;

Numbers, bigints, and strings become text. Booleans, null, and undefined render as empty content. Text signals and functions bind to a text node, and nested arrays are expanded in order. Create reactive DOM and use when and list within an active lifetime.

See components and children and lists and keys for examples.