Reference·@tilia/react·TypeScript & ReScript signatures

API Reference

The public surface of @tilia/react. Each entry gives the signature, behavior, and one minimal example.

leaf(fn)

ReactSince 3.0

Wrap a React component with exact render dependency tracking.

function leaf<T, U>(fn: (p: T) => U): (p: T) => U
let leaf: ('a => 'b) => 'a => 'b

leaf wraps a component and uses exact render boundaries to track reads of Tilia proxies during rendering.

When tracked keys change, the wrapped component re-renders. The API is equivalent to a higher-order component and is preferred over useTilia for React integration.

See the React overview and useComputed.

Example
import { leaf } from "@tilia/react";

const Counter = leaf(() => {
  return <p>{app.count}</p>;
});
open TiliaReact

@react.component
let make = leaf(() => {
  <p> {app.count->React.int} </p>
})

useTilia()

ReactSince 2.0

Track reactive reads in a React component render.

function useTilia(): void
let useTilia: unit => unit

useTilia enables reactive tracking for a component's current render. Call it at the top of the component.

Reads of Tilia proxies during rendering become dependencies. When one of those dependencies changes, the component re-renders. useTilia is the hook form; leaf is the preferred wrapper when possible.

See the React overview and the related useComputed hook.

Example
import { useTilia } from "@tilia/react";

function Counter() {
  useTilia();
  return <p>{app.count}</p>;
}
open TiliaReact

@react.component
let make = () => {
  useTilia()
  <p> {app.count->React.int} </p>
}

useComputed(fn)

ReactSince 2.0

Compute a React value and re-render only when the result changes.

function useComputed<T>(fn: () => T): T
let useComputed: (unit => 'a) => 'a

useComputed evaluates fn with reactive tracking and returns its result.

The hook compares successive computed results and re-renders when the result changes. This differs from plain render reads, which depend on every tracked key read during rendering.

Use it with useTilia or leaf in React components. See the React overview.

Example
import { useComputed, useTilia } from "@tilia/react";

function TodoRow({ todo }: { todo: { id: string } }) {
  useTilia();
  const selected = useComputed(() => app.selectedId === todo.id);
  return <div className={selected ? "selected" : ""} />;
}
open TiliaReact

@react.component
let make = (~todo) => {
  useTilia()
  let selected = useComputed(() => app.selectedId === todo.id)
  <div className={selected ? "selected" : ""} />
}

make(tilia)

ReactSince 1.0

Create a React API bound to a specific Tilia context.

function make(tilia: Tilia): TiliaReact
let make: tilia => tilia_react

make from @tilia/react builds a React integration object (useTilia, useComputed, leaf) from a provided core Tilia context.

Use it with core make when an application needs isolated reactive worlds that do not affect one another.

The package-level exports provide the default-context version; this function provides the context-bound version.

Example
import { make as makeCore } from "tilia";
import { make as makeReact } from "@tilia/react";

const ctx = makeCore();
const reactApi = makeReact(ctx);
void reactApi.useTilia;
let ctx = Tilia.make()
let reactApi = TiliaReact.make(ctx)
ignore(reactApi.useTilia)

TiliaReact

ReactSince 2.0

Context-bound React integration surface for Tilia.

type TiliaReact {
  useTilia: () => void;
  useComputed: <T>(fn: () => T) => T;
  leaf: <T, U>(fn: (p: T) => U) => (p: T) => U
}
type tilia_react = {
  useTilia: unit => unit,
  useComputed: 'a. (unit => 'a) => 'a,
  leaf: 'a 'b. ('a => 'b) => 'a => 'b
}

The TiliaReact and tilia_react types define the shape of the React API object returned by react make.

It groups useTilia, useComputed, and leaf for a single core context.

See leaf, useTilia, and useComputed.

Example
import { make as makeCore } from "tilia";
import { make as makeReact } from "@tilia/react";
import type { TiliaReact } from "@tilia/react";

const reactApi: TiliaReact = makeReact(makeCore());
void reactApi.leaf;
open TiliaReact

let reactApi: tilia_react = make(Tilia.make())
ignore(reactApi.leaf)