Reintent

@reintent/react

@reintent/react: React reads λ and state through an intent's internal adapter, and evaluates the same intent programs.

Classes

CatalogRejection

Defined in: packages/react/src/implement.ts

A frame or an event the catalog does not take: errors says each, as data.

Extends

  • Error

Constructors

Constructor
new CatalogRejection(what, errors): CatalogRejection;

Defined in: packages/react/src/implement.ts

Parameters
ParameterType
whatstring
errorsCatalogError[]
Returns

CatalogRejection

Overrides
Error.constructor

Properties

PropertyModifierType
errorsreadonlyCatalogError[]

Refused

Defined in: packages/react/src/root.ts

A write the intent refused: its receipt says why.

Extends

  • Error

Constructors

Constructor
new Refused(receipt): Refused;

Defined in: packages/react/src/root.ts

Parameters
ParameterType
receipt{ code: "refused" | "not-allowed" | "unknown-proposal" | "closed"; message: string; ok: false; }
receipt.code"refused" | "not-allowed" | "unknown-proposal" | "closed"
receipt.messagestring
receipt.okfalse
Returns

Refused

Overrides
Error.constructor

Properties

PropertyModifierType
receiptreadonlyobject
receipt.codereadonly"refused" | "not-allowed" | "unknown-proposal" | "closed"
receipt.messagereadonlystring
receipt.okreadonlyfalse

Type Aliases

Implementation

type Implementation = object;

Defined in: packages/react/src/implement.ts

A catalog and the components that draw its kinds: what implement gives.

Properties

PropertyModifierTypeDescription
[IMPLEMENTATION]readonlytrue-
catalogreadonlyHostCatalogThe catalog's contract: what each frame is checked against.
componentsreadonlyReadonly<Record<string, ComponentType<any>>>-

KindComponentProps

type KindComponentProps<D> = KindData<D> & KindEvents<D> & D["children"] extends "none" ? object : object;

Defined in: packages/react/src/implement.ts

The props a kind's component is drawn with: its data (defaults filled in), its events, its children.

Type Parameters

Type Parameter
D extends KindDef

KindComponents

type KindComponents<C> = { [K in keyof C["kinds"] as C["kinds"][K] extends DomKind ? never : K]: ComponentType<KindComponentProps<C["kinds"][K]>> } & { [K in keyof C["kinds"] as C["kinds"][K] extends DomKind ? K : never]?: ComponentType<KindComponentProps<C["kinds"][K]>> };

Defined in: packages/react/src/implement.ts

The components a catalog's kinds are drawn with, by kind: every kind, except the DOM catalog's, which are drawn as the element of their name unless a component is given for one.

Type Parameters

Type Parameter
C extends Catalog

ReintentActions

type ReintentActions<I> = Omit<IntentHandle<I>, "snapshot" | "subscribe" | "close">;

Defined in: packages/react/src/root.ts

What useReintent() gives: the root's operations, one object for as long as the root lives. A write resolves to its receipt, and rejects (Refused) when the intent refused it, so an await does not take a refusal for a success. The same for an intent here and one in a worker (0060 §4).

Type Parameters

Type ParameterDefault type
Iunknown

ReintentHost

type ReintentHost = object;

Defined in: packages/react/src/root.ts

A host's configuration for the roots under it (0045 §1): how kinds are drawn, and the model.

Properties

PropertyTypeDescription
components?ImplementationA catalog's implementation (implement). The DOM catalog by default.
model?ModelThe model a root mounted under it is created with. A mounted root keeps the one it had.
onControl?(control) => voidWhere a control a node sends goes (0043 §5).

ReintentState

type ReintentState<I> = HandleState<I> & Observation;

Defined in: packages/react/src/root.ts

What useReintent(select) reads in a root: the intent's state, and what this root's evaluation found.

Type Parameters

Type ParameterDefault type
Iunknown

RemoteViewSource

type RemoteViewSource = object;

Defined in: packages/react/src/root.ts

What a view of an intent elsewhere shows (remoteView of @reintent/lib/remote): the latest commit, and actions sent back with where they were offered (0060 §5).

Methods

act()
act(
   at, 
   offered, 
   taken?
): Promise<Receipt>;

Defined in: packages/react/src/root.ts

Parameters
ParameterType
atstring
offeredHostAction
taken?HostAction
Returns

Promise<Receipt>

latest()
latest(): 
  | {
  observed: Observation;
  tree: Rendered | Rendered[] | null;
}
  | undefined;

Defined in: packages/react/src/root.ts

Returns

| { observed: Observation; tree: Rendered | Rendered[] | null; } | undefined

subscribe()
subscribe(fn): () => void;

Defined in: packages/react/src/root.ts

Parameters
ParameterType
fn(commit) => void
Returns

() => void


RootProps

type RootProps<P> = { [K in keyof P]: [Extract<NonNullable<P[K]>, Content>] extends [never] ? NonNullable<P[K]> extends (args: never[]) => unknown ? never : P[K] : ReactNode } & object;

Defined in: packages/react/src/root.ts

A root's props in React: what is Content in the program is React content here.

Type Declaration

NameType
ref?Ref<ReintentActions<P>>

Type Parameters

Type Parameter
P

Slots

type Slots = Readonly<Record<string, ReactNode>>;

Defined in: packages/react/src/root.ts

Content for a view's slots, by slot name (0045 §5).

Variables

reactRealizer

const reactRealizer: Realizer;

Defined in: packages/react/src/realizer.ts

The kernel's renderOnce, by React: give it to createIntent({ realizer }) and React evaluates the intent program (its components run as React components), with the same λ.

Example

reply.intent.tsx
import { choices, createIntent, host, useChoose, type HostNode } from '@reintent/core';
import { reactRealizer } from '@reintent/react';

function Reply() {
  const tone = useChoose(choices('formal', 'warm'), {
    of: 'r1',
    name: 'tone',
    defaultValue: 'warm',
  });
  return host('reply', { tone });
}

// The same program, evaluated by React instead of the kernel: the same λ.
const intent = createIntent({ program: <Reply />, realizer: reactRealizer });
console.log((intent.realize().tree as HostNode).props.tone); // → 'warm'

Functions

implement()

function implement<C>(catalog, components): Implementation;

Defined in: packages/react/src/implement.ts

Implement a catalog in React: a component for each of its kinds. TypeScript checks each component's props against its kind (a data prop, an event, children), and a kind left out; the call checks the kinds again, and throws for one left out, one not in the catalog, or a value that is not a component. The DOM catalog's kinds may be left out: they are drawn as the element of their name.

Type Parameters

Type Parameter
C extends Catalog

Parameters

ParameterType
catalogC
componentsKindComponents<C>

Returns

Implementation

Example

hosts.tsx
import { z } from 'zod';
import { action, defineCatalog, kind } from '@reintent/catalog';
import { implement } from '@reintent/react';

export const card = defineCatalog(
  { id: 'card', version: 1 },
  {
    'card:tone': kind({
      about: 'the tone of a card, which a person picks',
      props: z.object({ tone: z.enum(['warm', 'formal']) }),
      events: { pick: action({ value: z.enum(['warm', 'formal']) }) },
    }),
  },
);

export const hosts = implement(card, {
  'card:tone': (p) => <button onClick={() => p.pick('formal')}>{p.tone}</button>,
});
card.intent.tsx
import { action, choices, host, useChoose } from '@reintent/core';

export function Card() {
  const tone = { of: 'card', name: 'tone' };
  const value = useChoose(choices('warm', 'formal'), { ...tone, defaultValue: 'warm' });
  return host('card:tone', { tone: value, pick: action.decide(tone) });
}
app.tsx
import { implement, ReintentProvider, reintentView } from '@reintent/react';
import { renderToString } from 'react-dom/server';
import { card, hosts } from './hosts.js';
import { Card } from './card.intent.js';

const CardView = reintentView(Card);
console.log(
  renderToString(
    <ReintentProvider components={hosts}>
      <CardView />
    </ReintentProvider>,
  ),
); // → '<button>warm</button>'
try {
  implement(card, {} as never);
} catch (e) {
  console.log((e as Error).message); // → "implement(card): no component for 'card:tone'"
}

ReintentProvider()

function ReintentProvider(p): ReactNode;

Defined in: packages/react/src/root.ts

Gives the roots under it how to draw (components) and the model a root is created with. It is React context: it holds no Intent, and two roots under it are two Intents.

Parameters

ParameterType
pReintentHost & object

Returns

ReactNode

Example

card.intent.tsx
import { host } from '@reintent/core';

export function Card(p: { to: string }) {
  return host('p', { children: `Hi ${p.to}!` });
}
app.tsx
import { renderToStaticMarkup } from 'react-dom/server';
import { ReintentProvider, reintentView } from '@reintent/react';
import { Card as CardProgram } from './card.intent.js';

const Card = reintentView(CardProgram);
console.log(
  renderToStaticMarkup(
    <ReintentProvider>
      <Card to="Ann" />
      <Card to="Bo" />
    </ReintentProvider>,
  ),
); // → '<p>Hi Ann!</p><p>Hi Bo!</p>'

reintentView()

function reintentView<P>(program, name?): (props) => ReactNode;

Defined in: packages/react/src/root.ts

A root as a React component (0045 §2): each mount owns an Intent of its own, made with the provider's model; its props are the root's input, and React content in them (children, a footer) is the content of a slot of that name. A root's props are data or React content: a function is not one, and a root given one throws (InvalidInput). The Intent lives as long as the mount does: hiding (<Activity>) keeps it; nothing disposes it but its being collected (§4.1).

Type Parameters

Type Parameter
P extends object

Parameters

ParameterTypeDescription
program(props) => unknown-
name?stringThe component's name in React's tools; the program's own by default.

Returns

(props) => ReactNode

Example

card.intent.tsx
import { host, type Content } from '@reintent/core';

export function Card(p: { to: string; children?: Content }) {
  return host('article', {
    children: [host('h1', { children: `Hi ${p.to}!` }), p.children],
  });
}
app.tsx
import { renderToStaticMarkup } from 'react-dom/server';
import { reintentView } from '@reintent/react';
import { Card as CardProgram } from './card.intent.js';

const Card = reintentView(CardProgram);
console.log(
  renderToStaticMarkup(
    <Card to="Ann">
      <button>Share</button>
    </Card>,
  ),
); // → '<article><h1>Hi Ann!</h1><button>Share</button></article>'

ReintentView()

function ReintentView(p): ReactNode;

Defined in: packages/react/src/root.ts

Shows an Intent held elsewhere (0045 §2): the advanced entry, for an instance made outside React, worked on from outside, or shown in several places. It does not set the input. Its children are its content for the slot named children, and slots for the others: this view's own, so two views of one Intent each show their own (§5).

Parameters

ParameterType
p| { intent: Intent<any>; remote?: undefined; view?: undefined; } | { intent?: undefined; remote: IntentHandle<any>; view: RemoteViewSource; } & object

Returns

ReactNode

Example

card.intent.tsx
import { host, type Content } from '@reintent/core';

export function Card(p: { to: string; children?: Content }) {
  return host('article', { children: [host('h1', { children: p.to }), p.children] });
}
app.tsx
import { createIntent, slot } from '@reintent/core';
import { ReintentView } from '@reintent/react';
import { renderToStaticMarkup } from 'react-dom/server';
import { Card } from './card.intent.js';

const card = createIntent({
  program: Card,
  input: { to: 'Ann', children: slot('children') },
});
console.log(
  renderToStaticMarkup(
    <>
      <ReintentView intent={card}>
        <em>first</em>
      </ReintentView>
      <ReintentView intent={card}>
        <b>second</b>
      </ReintentView>
    </>,
  ),
); // → '<article><h1>Ann</h1><em>first</em></article><article><h1>Ann</h1><b>second</b></article>'
card.dispose();

useIntent()

function useIntent(intent, opts?): Realization;

Defined in: packages/react/src/view.ts

Shows λ: the intent's realization in a context, read while rendering and read again when the intent changes. Once the view commits, what it asks for is sent (through the adapter): a view nobody shows (a render React drops) asks for nothing.

Parameters

ParameterType
intentIntent
opts{ provide?: Provide; }
opts.provide?Provide

Returns

Realization

Example

card.intent.tsx
import { choices, host, useChoose } from '@reintent/core';

export function Card() {
  const tone = useChoose(choices('warm', 'formal'), {
    of: 'card',
    name: 'tone',
    defaultValue: 'warm',
  });
  return host('p', { children: tone === 'warm' ? 'Hi Ann!' : 'Dear Ann,' });
}

export const card = <Card />;
app.tsx
import { createIntent, type HostNode } from '@reintent/core';
import { useIntent } from '@reintent/react';
import { renderToString } from 'react-dom/server';
import { card } from './card.intent.js';

const intent = createIntent({ program: card });

// A view that draws λ itself: here, the text of the card's paragraph.
function Greeting() {
  const p = useIntent(intent).tree as HostNode;
  return <h1>{p.children.join('')}</h1>;
}
console.log(renderToString(<Greeting />)); // → '<h1>Hi Ann!</h1>'
intent.decide({ of: 'card', name: 'tone' }, 'formal');
console.log(renderToString(<Greeting />)); // → '<h1>Dear Ann,</h1>'

Deprecated

Use useReintent (0060 §7): in a root, useReintent((s) => s.reads); λ is the root's to draw.


useIntentState()

function useIntentState<T>(intent, select): T;

Defined in: packages/react/src/view.ts

Selects from the intent's state: select(store.getState()), worked out again only when the state changes, so it may build a new value (a list, an object) each time. Nothing is sent: what can be undone, the facts told, the program's revision.

Type Parameters

Type Parameter
T

Parameters

ParameterType
intentIntent
select(state) => T

Returns

T

Example

card.intent.tsx
import { choices, host, useChoose } from '@reintent/core';

export function Card() {
  const tone = useChoose(choices('warm', 'formal'), {
    of: 'card',
    name: 'tone',
    defaultValue: 'warm',
  });
  return host('p', { children: tone });
}

export const card = <Card />;
app.tsx
import { createIntent } from '@reintent/core';
import { useIntentState } from '@reintent/react';
import { renderToString } from 'react-dom/server';
import { card } from './card.intent.js';

const intent = createIntent({ program: card });

// An undo button outside λ: it reads the history, and acts on the intent.
function Undo() {
  const canUndo = useIntentState(intent, (s) => s.history.canUndo);
  return (
    <button disabled={!canUndo} onClick={() => intent.history.undo()}>
      Undo
    </button>
  );
}
console.log(renderToString(<Undo />)); // → '<button disabled="">Undo</button>'
intent.decide({ of: 'card', name: 'tone' }, 'formal');
console.log(renderToString(<Undo />)); // → '<button>Undo</button>'

Deprecated

Use useReintent(intent, select) (0060 §7).


useReintent()

Call Signature

function useReintent<I>(): ReintentActions<I>;

Defined in: packages/react/src/root.ts

The root a component is in (0060 §7), or the Intent it names:

useReintent() the nearest root's operations useReintent((s) => …) reads it: the intent's state, and what this root's evaluation found (reads, unresolved), from the same commit as what it draws useReintent(card) the operations of an Intent named from outside a root (an Intent here, or a handle of one elsewhere) useReintent(card, (s) => …) reads its state; outside a root there is no evaluation to read

A selection renders again only when what it selects changes. Reading sends nothing.

Type Parameters
Type ParameterDefault type
Iunknown
Returns

ReintentActions<I>

Example
card.intent.tsx
import { choices, host, useChoose, type Content } from '@reintent/core';

export function Card(p: { children?: Content }) {
  const tone = useChoose(choices('warm', 'formal'), {
    of: 'card',
    name: 'tone',
    defaultValue: 'warm',
  });
  return host('article', { children: [host('p', { children: tone }), p.children] });
}
app.tsx
import { renderToStaticMarkup } from 'react-dom/server';
import { reintentView, useReintent } from '@reintent/react';
import { Card as CardProgram } from './card.intent.js';

const Card = reintentView(CardProgram);

// Beside the card: what its evaluation read, and a way to decide it.
function Formal() {
  const { decide } = useReintent();
  const tone = useReintent((s) => s.reads.find((r) => r.key === 'card#tone')?.source);
  return (
    <button onClick={() => decide({ of: 'card', name: 'tone' }, 'formal')}>{tone}</button>
  );
}
console.log(
  renderToStaticMarkup(
    <Card>
      <Formal />
    </Card>,
  ),
); // → '<article><p>warm</p><button>default</button></article>'

Call Signature

function useReintent<T, I>(select): T;

Defined in: packages/react/src/root.ts

The root a component is in (0060 §7), or the Intent it names:

useReintent() the nearest root's operations useReintent((s) => …) reads it: the intent's state, and what this root's evaluation found (reads, unresolved), from the same commit as what it draws useReintent(card) the operations of an Intent named from outside a root (an Intent here, or a handle of one elsewhere) useReintent(card, (s) => …) reads its state; outside a root there is no evaluation to read

A selection renders again only when what it selects changes. Reading sends nothing.

Type Parameters
Type ParameterDefault type
T-
Iunknown
Parameters
ParameterType
select(state) => T
Returns

T

Call Signature

function useReintent<I>(target): ReintentActions<I>;

Defined in: packages/react/src/root.ts

The root a component is in (0060 §7), or the Intent it names:

useReintent() the nearest root's operations useReintent((s) => …) reads it: the intent's state, and what this root's evaluation found (reads, unresolved), from the same commit as what it draws useReintent(card) the operations of an Intent named from outside a root (an Intent here, or a handle of one elsewhere) useReintent(card, (s) => …) reads its state; outside a root there is no evaluation to read

A selection renders again only when what it selects changes. Reading sends nothing.

Type Parameters
Type Parameter
I
Parameters
ParameterType
target| IntentHandle<I> | Intent<I>
Returns

ReintentActions<I>

Call Signature

function useReintent<T, I>(target, select): T;

Defined in: packages/react/src/root.ts

The root a component is in (0060 §7), or the Intent it names:

useReintent() the nearest root's operations useReintent((s) => …) reads it: the intent's state, and what this root's evaluation found (reads, unresolved), from the same commit as what it draws useReintent(card) the operations of an Intent named from outside a root (an Intent here, or a handle of one elsewhere) useReintent(card, (s) => …) reads its state; outside a root there is no evaluation to read

A selection renders again only when what it selects changes. Reading sends nothing.

Type Parameters
Type Parameter
T
I
Parameters
ParameterType
target| Intent<I> | IntentHandle<I>
select(state) => T
Returns

T

Last updated on

Edit on GitHub