Build interactive pages with Ilha
Write Ilha components, state, events, and islands in app pages.
Call atom and watch at the top level of each component, in the same order on every render. Never call them inside if, loops, callbacks, or after await.
import * as Atom from "effect/reactivity/Atom";
import { atom, watch } from "ilha";
export default function Counter({ start = 0 }: { start?: number }) {
const count = atom(start); // read count(), write count.set(1)
const doubled = atom(Atom.map(count.atom, (n) => n * 2)); // derived
const startedAt = atom.lazy(() => Date.now()); // per-instance init
watch.once(() => {
// mount-only side effect; return a cleanup if needed
});
return (
<button type="button" onclick={() => count.update((n) => n + 1)}>
{count} / {doubled} (since {startedAt})
</button>
);
}
Follow the atom rules
- Pass
atoma value, an Effect Atom, an Effect, or a Stream. - Never pass a plain function; use
atom.lazy(() => …)for init or stored functions. - Pass
handle.atomto Effect APIs that expect an atom handle. - Derive state with
atom(Atom.map(source.atom, (n) => …)). - Give every component returned from
.map()a stablekey. - Use
yield* when(…)only inside generator components. - Use lowercase DOM names:
onclick,class,for. - Keep island props JSON; never put JSX in atoms.
Bind reactive attributes by calling atoms
Attributes take plain values only. To make one reactive (hidden,
disabled, class, aria-*, value, checked), call the atom in the
render: the read subscribes the component, so it re-renders with fresh
values when the atom changes and the morph syncs the attribute. It also
renders on the server, because the read returns the current value.
import { atom } from "ilha";
export default function Seat() {
const mine = atom(false);
return (
<button
type="button"
disabled={mine()}
aria-pressed={String(mine())}
class={mine() ? "seat seat-mine" : "seat"}
onclick={() => mine.set(true)}
>
{mine() ? "Yours" : "Take it"}
</button>
);
}
Only value, checked (and selected on option) also accept the bare
handle, for a live two-way binding that skips re-render (value={text}).
Call it instead (value={text()}) for a controlled value that follows
state and undoes user edits.
What not to do:
hidden={() => closed()}— a plain function is never an attribute value; it never paints and the types reject it.hidden={closed}— a bare handle binds only throughvalue/checked/selected; everywhere else it is dropped.aria-busy={busy}— same trap; writearia-busy={String(busy())}.
Render islands on island pages
- Name the page
*.island.tsxfor static HTML with hydrated islands only. - Import islands with
import X from "./_x" with { island: "visible" }. - Use
useIslandForm()from@ilha/voidon island pages, notuseForm(). - Assert server output with
renderHtml(Component)fromilha/test.
Read the error code first
Every Ilha error carries a code such as ILHA002, a Fix: line, and a link. Read the fix before changing code.
For the person-side walkthrough, see Add interactive pages.