Reintent
/

<Branch>

Reads a choice once and evaluates the case it took.

function Branch<T>(p): IntentElement;

Defined in: packages/core/src/intent/handlers.ts

Pattern matching (0033 §3.1): reads the choice once and evaluates the case it took. λ holds a branch host node with the tag it took, the kinds there are, and that case. Inside <World each>, the choice is enumerated, and each case is a path of its own.

Type Parameters

Type Parameter
T extends Tagged

Parameters

ParameterType
pBranchProps<T>

Returns

IntentElement

Example

import { Branch, choices, createIntent, host, type HostNode } from '@reintent/core';

// A choice of subtree: λ holds a branch node with the case the choice collapsed to.
function Gallery(p: { id: string }) {
  return (
    <Branch
      on={choices('grid', 'list')}
      of={p.id}
      name="view"
      defaultValue="list"
      cases={{ grid: host('grid', { columns: 3 }), list: host('list') }}
    />
  );
}

const intent = createIntent({ program: <Gallery id="g1" /> });
const branch = intent.realize().tree as HostNode;
console.log(branch.kind, branch.props.chosen); // → 'branch' 'list'

intent.decide({ of: 'g1', name: 'view' }, 'grid');
const chosen = (intent.realize().tree as HostNode).children[0] as HostNode;
console.log(chosen.kind, chosen.props); // → 'grid' { columns: 3 }

Last updated on

Edit on GitHub