AsterDocsv1.2.3

Docs / Guides

Components and state

A click counter can keep its value in a local variable inside a generator. When two components need to read and change the same value, put it in a signal and read it during rendering.


1. Stateless Components (function)

If a component only transforms props into markup, write an ordinary JavaScript function. For example, this badge reads two props and returns a span.

tsx
export function Badge({ text, tone = 'neutral' }: { text: string; tone?: string }) {
  return <span class={`pill ${tone}`}>{text}</span>;
}

2. Stateful Components (function*)

Declare instance state before the loop. Each advance through the loop sees the current props and can yield a view.

tsx
import { type Ctx } from '@fierst/aster';

export function* Counter(
  props: { start?: number; label?: string },
  ctx: Ctx<{ start?: number; label?: string }>,
) {
  // Setup (runs once)
  let count = props.start ?? 0;

  try {
    // Render loop (runs on every event, prop change, or shared signal update)
    for (props of ctx) {
      const doubled = count * 2;

      if (count < 0) {
        yield <p>Count went negative!</p>;
        continue;
      }

      yield (
        <button onClick={() => count++}>
          {props.label ?? 'Clicks'}: {count} (2× = {doubled})
        </button>
      );
    }
  } finally {
    // Teardown (runs automatically when unmounted via gen.return())
    console.log('Counter unmounted');
  }
}

How handlers read local variables

The click handler and the render loop share the same count variable. The handler increments it. On the next pass, the loop computes doubled from its new value. Conditional expressions such as disabled={!draft.trim()} work the same way. For lists, use a stable key when a row should retain its state through reordering. Try changing props and updating a list.


Props across updates

Change the label and step while keeping the current count.

3. Two-Way Form Bindings (bind:*)

@fierst/aster/vite (src/compiler.ts) lowers bind:value={draft}, bind:checked={task.done}, and bind:open={isOpen} into getter/setter tuples automatically:

tsx
export function* SearchBox() {
  let query = '';
  for (;;) {
    yield (
      <div>
        <input bind:value={query} placeholder="Search..." />
        <button disabled={!query.trim()} onClick={() => (query = '')}>
          Clear
        </button>
      </div>
    );
  }
}

Use a mutable local, a writable property, a signal held in a const or import, or an explicit [get, set] pair. For example, const query = signal('') works with bind:value={query}. Calling query() there supplies only its current value. Both compilers flag a known scalar at its JSX source location. A factory returning a binding is checked during server rendering or when the control mounts.

DOM handlers and custom binding setters can throw or return a rejecting promise. Aster forwards that failure to the nearest error boundary around the component that rendered the listener, or logs the event or binding name when there is no boundary. Return or await asynchronous callbacks to keep their errors in the event chain.

Use on:keydown={event => event.key === 'Escape' && event.currentTarget.blur()} for a native keyboard event. TypeScript infers both the event and the element. For application-defined events, augment the DOM interface GlobalEventHandlersEventMap with the event name and its CustomEvent<Detail> type to infer event.detail in JSX.

Bindings and keyed rows

Add a task and change its completion state.

4. Shared Cross-Component State (signal())

When multiple components (including Web Components) need to stay in sync, create a signal() in a shared module. Reading step() synchronously during rendering subscribes that component to changes:

tsx
import { signal } from '@fierst/aster';

export const step = signal(1);

export function* StepDisplay() {
  for (;;) {
    // Reading step() during rendering subscribes this component.
    yield <span>Current step: +{step()}</span>;
  }
}

5. Async Components (async function*)

Use async function* to express loading states and sequential fetches as straight-line code. Every component receives ctx.signal (AbortSignal), which aborts immediately on unmount:

tsx
export async function* UserProfile(props: { id: string }, ctx: Ctx) {
  yield <div class="skeleton">Loading user #{props.id}…</div>;

  const res = await fetch(`/api/users/${props.id}`, { signal: ctx.signal });
  const user = await res.json();

  yield (
    <article>
      <h3>{user.name}</h3>
      <p>{user.bio}</p>
    </article>
  );
}

Recovering from a failed request

Choose a name, then simulate a 503. Retry from the error message to load a working profile.

Tracing a component lifetime

Change or finish a request and inspect the order of cleanup events.

6. Spread Props, Style Objects, and JSX Comments

Here a component gathers remaining props and forwards them to the button it renders:

tsx
export function ActionButton({ label, ...rest }: { label: string; [key: string]: any }) {
  return (
    <button {...rest} style={{ fontWeight: 600 }}>
      {/* The JSX compiler removes this comment. */}
      {label}
    </button>
  );
}

7. Tree-Scoped Context (createContext, provide, useContext, <Provider>)

Use module-level signal() for client-wide state, and tree-scoped Context when passing data down a subtree or rendering concurrently on the server:

tsx
import { createContext, provide, useContext, Provider } from '@fierst/aster';

export const UserCtx = createContext<{ name: string } | null>(null);

export function Greeting() {
  const user = useContext(UserCtx);
  return <p>Hello, {user ? user.name : 'Guest'}</p>;
}

export function App({ user }: { user: { name: string } }) {
  // Option A: imperative inside a parent component
  provide(UserCtx, user);
  return <Greeting />;

  // Option B: declarative wrapper
  // return <Provider context={UserCtx} value={user}><Greeting /></Provider>;
}

Automatic async function* Prop Restarts

Aster compares each prop with Object.is. Changing id in <UserProfile id={selectedId} /> aborts the previous execution and starts a new one. Pass ctx.signal to the pending operation so it can settle and let the old generator reach its finally block.

Function props participate in this comparison. If a parent passes load={loadProfile}, replacing the loader restarts the child. Declare callbacks before the parent generator's render loop when they should keep the same identity across updates. An inline arrow inside the loop creates a fresh function on every render.

Start a request in the execution trace, change the selection, then leave a delegated confirmation. Compare the abort message with the order of the two cleanup messages.