---
title: Build interactive pages with Ilha
description: 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`.

```tsx
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

1. Pass `atom` a value, an Effect Atom, an Effect, or a Stream.
2. Never pass a plain function; use `atom.lazy(() => …)` for init or stored functions.
3. Pass `handle.atom` to Effect APIs that expect an atom handle.
4. Derive state with `atom(Atom.map(source.atom, (n) => …))`.
5. Give every component returned from `.map()` a stable `key`.
6. Use `yield* when(…)` only inside generator components.
7. Use lowercase DOM names: `onclick`, `class`, `for`.
8. 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.

```tsx
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 through `value`/`checked`/`selected`; everywhere else it is dropped.
- `aria-busy={busy}` — same trap; write `aria-busy={String(busy())}`.

## Render islands on island pages

1. Name the page `*.island.tsx` for static HTML with hydrated islands only.
2. Import islands with `import X from "./_x" with { island: "visible" }`.
3. Use `useIslandForm()` from `@ilha/void` on island pages, not `useForm()`.
4. Assert server output with `renderHtml(Component)` from `ilha/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](/es/frontend/interactive-pages).
