Familiar patterns
Reze components run once. Their JavaScript XML (JSX) reads signals through bindings, so an update reruns only the binding that reads the changed value. Code written for a framework that reruns components often still compiles. Some of those forms give up updates, and a few stop updating without any diagnostic. This page covers each pattern, what it costs, and what to write instead.
| Pattern | Works | Cost | Write instead |
|---|---|---|---|
| Destructured props | Yes, for plain names and simple defaults | A default that calls a function reads every prop once and warns | props.x, with ?? for fallbacks |
{items.map(...)} | Yes | Each change rebuilds every row and loses the row's DOM state | <For each={items}> |
key on a native element | Renders as an attribute and warns | Does not key rows | <For> with keyed |
const copy = props.x in the body | Yes | copy keeps the value from the first run | Read props.x in JSX, or use computed |
Mutating an array in a signal | Yes | Nothing notifies, so the UI keeps the old data | Assign a new array, or use store |
className | Renders as an attribute and warns | The browser ignores the attribute | class |
useState, useEffect, useMemo | Not exported | Not available | signal, effect, computed |
Destructured props
The compiler rewrites a destructured parameter so each use reads the current prop. Changing a value that a child receives updates the child's output, and the child does not run again:
import { signal } from "reze-js";
function Link({ href, label = "home", ...rest }: { href: string; label?: string; title?: string }) {
return (
<a href={href} {...rest}>
{label}
</a>
);
}
export function Nav() {
let target = signal("/a");
return (
<div>
<Link href={target} title="t" />
<button onClick={() => (target = "/b")}>go</button>
</div>
);
}
The rewrite covers plain names, defaults made of literals or other side-effect-free expressions, and ...rest. The compiler reports it as the informational PROPS_REWRITTEN.
A default replaces only undefined. Passing null keeps null, which renders nothing. To replace null too, read the prop in the body with ??:
export function Link(props: { href: string; label?: string }) {
return <a href={props.href}>{props.label ?? "home"}</a>;
}
The compiler reports PROPS_DESTRUCTURED as a warning when it cannot rewrite the parameter. Every value in that parameter is then read once, when the component runs, and never updates. The message names one of these reasons:
default: a default that is not a literal or side-effect-free expression, such aslabel = fallback().computed-key: a computed key, such as{ [name]: value }.nested-default: a default on a nested pattern, such as{ user: { name } = {} }.nested-rest: a rest element inside a nested pattern.array-pattern: an array pattern as the parameter.written: the body assigns to a destructured name.arguments: the body usesarguments.generator: the component is a generator.params: the component has more than one parameter.type: a type reads a destructured key that is not an identifier, such as"data-id".
This sample adds a function call to the default, so the compiler reports default:
function fallback() {
return "home";
}
export function Link({ href, label = fallback() }: { href: string; label?: string }) {
return <a href={href}>{label}</a>;
}
The whole parameter becomes read-once, not only label. For defaults, splitting, and merging, use mergeProps, splitProps, and omitProps. The props section of Components and JSX describes them.
Lists with map
An expression such as {items.map((name) => <li>{name}</li>)} is a binding like any other. When items changes, map runs again and its result replaces every mapped row. Static siblings keep their nodes:
import { signal } from "reze-js";
export function List() {
let items = signal(["x", "y"]);
return (
<div>
<ul>
<li>first</li>
{items.map((name) => (
<li>{name}</li>
))}
<li>last</li>
</ul>
<button onClick={() => (items = [...items, "z"])}>add</button>
</div>
);
}
After the click, first and last keep their nodes, and x and y are new nodes. Rebuilt rows lose any state their elements hold, such as typed input, focus, or scroll position. Cleanups registered with onCleanup inside a row run when the array changes. Chains such as .filter(...).map(...) rebuild rows the same way.
<For> keeps a row while its item stays in the list, moves the row's nodes when the order changes, and disposes only the rows that were removed:
import { For, signal } from "reze-js";
export function List() {
let items = signal(["x", "y"]);
return (
<div>
<ul>
<For each={items}>{(name) => <li>{name}</li>}</For>
</ul>
<button onClick={() => (items = [...items, "z"])}>add</button>
</div>
);
}
Use map for lists whose rows hold no state. Use <For> for everything else. Pass keyed a function to match rows by a key instead of by item identity. The row then reads its item as a signal, and replacing an item with another object that has the same key keeps the row's nodes:
import { For, signal } from "reze-js";
type Todo = { id: number; text: string };
export function Todos() {
let todos = signal<Todo[]>([{ id: 1, text: "a" }]);
return (
<div>
<ul>
<For each={todos} keyed={(todo) => todo.id}>
{(todo) => <li>{todo.text}</li>}
</For>
</ul>
<button onClick={() => (todos = [{ id: 1, text: "b" }])}>go</button>
</div>
);
}
For more about row identity and the three For modes, see Building interfaces.
A key attribute on a native element does not key anything. The compiler reports KEY_ON_ELEMENT and renders key as an attribute:
export function Rows() {
return (
<ul>
<li key="a">a</li>
</ul>
);
}
Values read once in the body
The body of a component runs once, so a value read there keeps its first value. This component copies props.text in the body, and the caller later changes it:
import { signal } from "reze-js";
function Label(props: { text: string }) {
const copy = props.text;
return (
<p>
{copy} / {props.text}
</p>
);
}
export function Parent() {
let text = signal("a");
return (
<div>
<Label text={text} />
<button onClick={() => (text = "b")}>go</button>
</div>
);
}
After the click, the paragraph reads a / b. The first part keeps the old value, and the JSX read follows the caller. Reads in JSX stay live, and so does a computed built from a prop:
import { computed, signal } from "reze-js";
function Upper(props: { text: string }) {
const upper = computed(props.text.toUpperCase());
return <p>{upper}</p>;
}
export function Parent() {
let text = signal("a");
return (
<div>
<Upper text={text} />
<button onClick={() => (text = "b")}>go</button>
</div>
);
}
The paragraph reads A first and B after the click.
In-place mutation
A signal notifies its readers when you assign a new value, not when you change the value it holds. items.push(x) changes the array without notifying anyone. A store tracks writes to its properties and array methods:
import { signal, store } from "reze-js";
export function Bag() {
let items = signal(["x"]);
let tags = store(["x"]);
return (
<div>
<p>{items.length}</p>
<p>{tags.length}</p>
<button onClick={() => items.push("y")}>push</button>
<button onClick={() => tags.push("y")}>push tag</button>
<button onClick={() => (items = [...items])}>copy</button>
</div>
);
}
After push the first paragraph still reads 1. After copy it reads 2, because the assignment notifies. After push tag the second paragraph reads 2, because store tracks the push. For signal and store in detail, see Reactivity.
Attribute names
Attribute names follow HTML. className is not an attribute, so the compiler renders it as written and reports UNKNOWN_ATTRIBUTE:
export function Row() {
return <div className="row">x</div>;
}
The rendered element has the attribute classname, and the browser ignores it. Write class.
A lowercase event attribute such as onclick={handler} still attaches a listener in the compiled output, but the compiler reports it as EVENT_NAME_LOWERCASE. Write the camel-case name:
function handler() {}
export function Action() {
return <button onclick={handler}>go</button>;
}
Hooks
Reze exports no useState, useEffect, or useMemo. Declarations in a component body cover the same jobs, and each one runs when the component runs:
| Instead of | Write |
|---|---|
useState(0) | let count = signal(0) |
useMemo(() => count * 2) | const doubled = computed(count * 2) |
useEffect(() => {...}) | effect(() => {...}), which runs once on mount and again after the signals it reads change. See Lifecycle. |
useContext(Ctx) | useContext(Ctx) is a plain function that reads the nearest provider above it. It does not depend on call order. See Lifecycle. |
Related pages
- Components and JSX for props, children, and the
propshelpers. - Reactivity for
signal,computed,effect, andstore. - Lifecycle for effects, cleanup, and context.
- Building interfaces for lists, conditions, and events.
- The compiler for diagnostics and what gets compiled.