Guide

Dynamic paths

Each hole patches when a model path it reads changes. Most of the time the compiler finds those paths in the source. When it cannot, the server records what the hole reads while it renders.

#Paths the compiler finds

The compiler reads the hole and the helpers it calls:

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

function plural(count: number, word: string): string {
  return count === 1 ? word : `${word}s`;
}

export default function Cart(): JSX.Element {
  let count = 1;
  let note = '';

  function add() {
    count = count + 1;
  }
  function setNote(text: string) {
    note = text;
  }

  return (
    <main>
      <p>
        {count} {plural(count, 'item')}
      </p>
      <input value={note} on:input={(e) => setNote(e.currentTarget.value)} />
      <button on:click={add}>Add</button>
    </main>
  );
}

plural(count, 'item') reads count, so typing in the field does not touch it.

#Paths the server records

A helper can take an object built from several state fields, and read only some of them:

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

interface View {
  mode: string;
  a: string;
  b: string;
}

function pick(view: View): string {
  return view.mode === 'a' ? view.a : view.b;
}

export default function Pick(): JSX.Element {
  let mode = 'a';
  let a = 'first';
  let b = 'second';

  const view = { mode, a, b };

  function setMode(value: string) {
    mode = value;
  }
  function setA(value: string) {
    a = value;
  }
  function setB(value: string) {
    b = value;
  }

  return (
    <main>
      <p>{pick(view)}</p>
      <button on:click={() => setMode('b')}>Mode b</button>
      <button on:click={() => setA('first!')}>A</button>
      <button on:click={() => setB('second!')}>B</button>
    </main>
  );
}

The compiler cannot tell which fields pick reads, so {pick(view)} is a dynamic hole. The same holds for a stateless component that passes its whole props to a helper.

All dynamic holes of a module share one binding chunk, $dynamic. Every handler that writes a path loads it with the handler chunk, whether a hole read that path or not.

#Read precisely

#Inside rows, branches and children

Inside a <For>, a condition, a <Try> or a child component, a dynamic hole uses the paths the compiler finds in its arguments instead. For pick(view) in a row that is mode, a and b: the hole runs on any change to one of them. The result is right but the hole runs more often.

#Errors

See Component syntax for the hole grammar.