Guide

Nesting components

A component can render another component that has state and handlers. The child gets its props from the parent, keeps its own model, and sends messages to the parent through callback props. A click inside a child loads only the child's chunks. The samples on this page are examples/counters.

#A child component

Counter has state (count), two props values (label, start) and one callback prop (onRemove):

tsx
import type { JSX } from 'idlip/jsx-runtime';

export default function Counter(props: {
  label: string;
  start: number;
  onRemove: () => void;
}): JSX.Element {
  let count = props.start;

  function increment() {
    count = count + 1;
  }

  return (
    <div>
      <span>{props.label}</span>
      <button on:click={increment}>+</button>
      <output>{count}</output>
      <button on:click={props.onRemove}>Remove</button>
    </div>
  );
}

increment is the child's own handler. It changes count in the child's model and nothing in the parent's. The Remove button puts the callback prop in its event attribute, so the click runs a handler of the parent.

#The parent

Board imports Counter and renders one per item in a keyed <For>:

tsx
import { For } from 'idlip';
import type { JSX } from 'idlip/jsx-runtime';
import Counter from './counter.tsx';

interface Item {
  id: string;
  label: string;
  start: number;
}

export default function Board(props: { items: Item[] }): JSX.Element {
  let items = props.items;

  const total = items.length;

  function remove(id: string) {
    items = items.filter((item) => item.id !== id);
  }

  return (
    <main>
      <p>{total} counters</p>
      <For each={items} key={(c) => c.id}>
        {(c) => <Counter label={c.label} start={c.start} onRemove={() => remove(c.id)} />}
      </For>
    </main>
  );
}

Only the root component goes to render() on the server: render(Board, { items }). The Vite plugin finds Counter because Board imports it and renders it in JSX, and compiles it as a child.

#Props are data

A prop is a hole in the closed grammar of views. The parent computes it and the child reads it as props.label.

#Messages from a child

The child uses a callback prop in an event attribute: on:click={props.onRemove}. The server writes the parent's message into the button:

html
<button on:click='["remove",["a1"],"",""]' i-a="2">Remove</button>

A click on it runs remove("a1") on the parent's model. The parent patches its count and its list, and the list drops the child's block.

A click on + sends increment with no path in the attribute. The loader reads the child's path from the boundary around the button and runs increment on that child's model.

#Slots

Each child has a slot: its place in the parent, written as a path. The server marks each child with a boundary comment that holds the path:

html
<!--n:0:a1-->
<div>…</div>
<!--/n-->

The state script holds one flat table of slots, each with the child's module, model and props:

json
{
  "0:a1": {
    "module": "/src/counter.tsx",
    "model": { "count": 0 },
    "props": { "label": "Apples", "onRemove": ["remove", ["a1"], "", ""] }
  }
}

A parent handler cannot read a child's state. It sees only its own model.

#What the browser loads

A child compiles to its own chunks, as a root does: one per handler and one per binding group. The first click on + in a child loads two chunks:

Chunk Why
counter.handler.increment.<hash>.js The handler of the click.
counter.binding.count.<hash>.js The binding of the path it writes.

No chunk of Board loads. A click on + in another counter then loads nothing, since both chunks are in the page. The e2e tests in e2e/tests/nested.spec.ts count these requests and compare the DOM with a fresh server render after each click and each remove.

A child that a project module renders also gets a view chunk. When the parent adds a row in the browser, the list chunk imports it to render the new child. The server render and a click on an existing child never load it.

The loader holds the nesting code behind a build flag. The plugin turns it on when a module renders a child, which adds about 650 B with gzip to the loader. A page with no children does not pay for it.

#Several components in one file

A parent and its children can share a file. The default export, each named export, and each component that one of them renders are components of their own:

tsx
import { For } from 'idlip';
import type { JSX } from 'idlip/jsx-runtime';

interface Item {
  id: string;
  label: string;
}

export function Counter(props: { start: number }): JSX.Element {
  let count = props.start;

  function increment() {
    count = count + 1;
  }

  return <button on:click={increment}>{count}</button>;
}

function Row(props: { label: string }): JSX.Element {
  let done = false;

  function toggle() {
    done = !done;
  }

  return (
    <li class={{ done }}>
      <button on:click={toggle}>{props.label}</button>
    </li>
  );
}

export default function Dashboard(props: { items: Item[] }): JSX.Element {
  return (
    <main>
      <Counter start={1} />
      <ul>
        <For each={props.items} key={(item) => item.id}>
          {(item) => <Row label={item.label} />}
        </For>
      </ul>
    </main>
  );
}

Row is not exported, so only this file renders it. Each component has a module id and chunk names of its own:

Component Module id A chunk of it
Dashboard /src/widgets.tsx widgets.list.1.<hash>.js
Counter /src/widgets.tsx#Counter widgets.Counter.handler.increment.<hash>.js
Row /src/widgets.tsx#Row widgets.Row.handler.toggle.<hash>.js

A first click on a Counter loads widgets.Counter.handler.increment and widgets.Counter.binding.count, and nothing of Dashboard or Row.

Another file imports a named component by name and renders it as a child, with the same slots and messages as a default import:

tsx
import { Counter } from './widgets.tsx';

export default function Page() {
  return <Counter start={5} />;
}

A component must keep its name when it is exported. export { Counter as Tally } is the error component-export, since the name is part of the chunk names. Import it under another name instead: import { Counter as Tally } from './widgets.tsx'.

#Limits