# View API

> Reference for UI nodes, JSX primitives, lists, attributes, and mounting.

Source: https://xote.dev/docs/api/view · Xote 7.2.0

This page lists the supported API for the `View` module.

`Node` is intentionally not expanded here. It currently exists as a compatibility alias, but new code and docs should use `View`.

## View

<a id="view-module" />

`View` is the UI node API used by JSX components and lower-level rendering helpers.

### View types

```rescript
type attrValue =
  | Static(string)
  | SignalValue(Signal.t<string>)
  | Compute(unit => string)

type node
```

`View.node` is the renderable value returned by components. `View.attrValue` is the supported attribute source type.

<a id="view-attr" />

### View.attr

```rescript
let attr: (string, string) => (string, View.attrValue)
```

Create a static attribute entry.

<a id="view-signalattr" />

### View.signalAttr

```rescript
let signalAttr: (string, Signal.t<string>) => (string, View.attrValue)
```

Create a reactive attribute from a string signal.

<a id="view-computedattr" />

### View.computedAttr

```rescript
let computedAttr: (string, unit => string) => (string, View.attrValue)
```

Create a reactive attribute from a compute function.

<a id="view-attr-module" />

### View.Attr

```rescript
View.Attr.string: (string, string) => (string, View.attrValue)
View.Attr.signal: (string, Signal.t<string>) => (string, View.attrValue)
View.Attr.compute: (string, unit => string) => (string, View.attrValue)
```

Namespaced aliases for [`View.attr`](#view-attr), [`View.signalAttr`](#view-signalattr), and [`View.computedAttr`](#view-computedattr).

<a id="view-text" />

### View.text

```rescript
let text: string => View.node
```

Create a static text node.

<a id="view-int" />

### View.int

```rescript
let int: int => View.node
```

Create a static integer text node.

<a id="view-float" />

### View.float

```rescript
let float: float => View.node
```

Create a static float text node.

<a id="view-bool" />

### View.bool

```rescript
let bool: bool => View.node
```

Create a static boolean text node.

<a id="view-signaltext" />

### View.signalText

```rescript
let signalText: (unit => string) => View.node
```

Create a reactive text node from a compute function.

<a id="view-signalint" />

### View.signalInt

```rescript
let signalInt: (unit => int) => View.node
```

Create a reactive integer text node from a compute function.

<a id="view-signalfloat" />

### View.signalFloat

```rescript
let signalFloat: (unit => float) => View.node
```

Create a reactive float text node from a compute function.

<a id="view-fragment" />

### View.fragment

```rescript
let fragment: array<View.node> => View.node
```

Group several nodes without adding a wrapper element.

<a id="view-signalfragment" />

### View.signalFragment

```rescript
let signalFragment: Signal.t<array<View.node>> => View.node
```

Render an array of nodes from a signal and replace the fragment when it changes.

<a id="view-tracked" />

### View.tracked

```rescript
let tracked: (unit => View.node) => View.node
```

Create an auto-tracked reactive block. Every signal read while the body runs subscribes the block, and the body re-evaluates when any dependency changes. Dependencies are re-discovered on each run, so conditional reads work. The block's children are replaced wholesale (no diffing) on update — keep tracked blocks small and use [`View.For`](#view-for) or [`View.eachWithKey`](#view-eachwithkey) for lists.

<a id="view-each" />

### View.each

```rescript
let each: (Signal.t<array<'a>>, 'a => View.node) => View.node
```

Render each item from a reactive array. Updates re-render the list contents.

<a id="view-list" />

### View.list

```rescript
let list: (Signal.t<array<'a>>, 'a => View.node) => View.node
```

Alias for [`View.each`](#view-each).

<a id="view-eachwithkey" />

### View.eachWithKey

```rescript
let eachWithKey: (Signal.t<array<'a>>, 'a => string, 'a => View.node) => View.node
```

Render a reactive array with keyed DOM reconciliation.

<a id="view-keyedlist" />

### View.keyedList

```rescript
let keyedList: (Signal.t<array<'a>>, 'a => string, 'a => View.node) => View.node
```

Alias for [`View.eachWithKey`](#view-eachwithkey).

<a id="view-for" />

### View.For

```rescript
module For: {
  type props<'item> = {
    each: MaybeSignal.t<array<'item>>,
    by?: 'item => string,
    render: 'item => View.node,
  }

  let make: props<'item> => View.node
}
```

JSX list primitive. Pass `by` to enable keyed reconciliation.

<a id="view-keyedfor" />

### View.KeyedFor

```rescript
module KeyedFor: {
  type props<'item> = {
    each: MaybeSignal.t<array<'item>>,
    by: 'item => string,
    render: 'item => View.node,
  }

  let make: props<'item> => View.node
}
```

JSX list primitive that always requires a key function.

<a id="view-show" />

### View.Show

```rescript
module Show: {
  type props = {
    when_: MaybeSignal.t<bool>,
    children?: View.node,
    fallback?: View.node,
  }

  let make: props => View.node
}
```

JSX conditional primitive for boolean branches.

<a id="view-maybe" />

### View.Maybe

```rescript
module Maybe: {
  type props<'value> = {
    value: MaybeSignal.t<option<'value>>,
    render: 'value => View.node,
    fallback?: View.node,
  }

  let make: props<'value> => View.node
}
```

JSX conditional primitive for option values.

<a id="view-value" />

### View.Value

```rescript
module Value: {
  type props<'value> = {
    value: MaybeSignal.t<'value>,
    render: 'value => View.node,
  }

  let make: props<'value> => View.node
}
```

JSX primitive that re-renders a node from one static or reactive value.

<a id="view-suspense" />

### View.Suspense

**New in 7.2.**

```rescript
module Suspense: {
  type props = {
    fallback?: View.node,
    children?: View.node,
  }

  let make: props => View.node
}
```

Shows `fallback` until every `Resource` created below it has loaded once. The children render hidden meanwhile, and their refs and `onMount` callbacks wait until they are shown. See [Resources](/docs/learn/async-data).

<a id="view-ref" />

### View.Ref

**New in 7.2.**

```rescript
module Ref: {
  type t = View.elementRef

  let make: unit => t
  let fromCallback: (Dom.element => unit) => t
  let get: t => option<Dom.element>
  let peek: t => option<Dom.element>
}
```

A handle on the DOM element a tag renders to. Pass it as `ref` (`<input ref=field />`, or `~ref` on `View.element`). The element is handed over once it is in the document, before any `onMount` runs, and the ref reads `None` again once the element is removed. `get` is a tracked read; `fromCallback` also calls a function with each element.

<a id="view-onmount" />

### View.onMount

**New in 7.2.**

```rescript
let onMount: (unit => unit) => unit
```

Runs a callback once the calling component's DOM is in the document. Call it in a component body. It runs untracked, under the component's context and error boundary, and never during server rendering.

<a id="view-oncleanup" />

### View.onCleanup

**New in 7.2.**

```rescript
let onCleanup: (unit => unit) => unit
```

Runs a callback when the calling component is removed. Call it in a component body or inside its `onMount`.

<a id="view-portal" />

### View.Portal

**New in 7.2.**

```rescript
module Portal: {
  type props = {
    target?: Dom.element,
    children?: View.node,
  }

  let make: props => View.node
}
```

Renders its children into `target` (`document.body` if omitted), and nothing where it stands; they are in the target before any `onMount` of the render runs. The children keep the portal's context and error boundary, and are removed with it.

<a id="view-errorboundary" />

### View.ErrorBoundary

**New in 7.2.**

```rescript
module ErrorBoundary: {
  type props = {
    fallback: (exn, unit => unit) => View.node,
    onError?: exn => unit,
    children?: View.node,
  }

  let make: props => View.node
}
```

Catches what its subtree throws while rendering or updating and renders `fallback` instead. `fallback` receives the failure and a `reset` function that renders the subtree again; `onError` is called with every failure caught.

<a id="view-element" />

### View.element

```rescript
let element: (
  string,
  ~attrs: array<(string, View.attrValue)>=?,
  ~events: array<(string, Dom.event => unit)>=?,
  ~children: array<View.node>=?,
  ~ref: View.elementRef=?,
  unit,
) => View.node
```

Create an element node for any tag.

<a id="view-null" />

### View.null

```rescript
let null: unit => View.node
```

Create an empty placeholder text node.

<a id="view-empty" />

### View.empty

```rescript
let empty: unit => View.node
```

Alias for [`View.null`](#view-null).

<a id="view-mount" />

### View.mount

```rescript
let mount: (View.node, Dom.element) => unit
```

Render a node into a DOM element.

<a id="view-mountbyid" />

### View.mountById

```rescript
let mountById: (View.node, string) => unit
```

Look up a DOM element by id and mount into it.

<a id="view-text-component" />

### View.Text

```rescript
<View.Text>{value}</View.Text>
<View.Text value={value} />
```

JSX text primitive. `value` or `children` may be a raw string, `Signal.t<string>`, `MaybeSignal.t<string>`, or `unit => string`.

<a id="view-int-component" />

### View.Int

```rescript
<View.Int>{value}</View.Int>
<View.Int value={value} />
```

JSX integer text primitive. `value` or `children` may be a raw int, `Signal.t<int>`, `MaybeSignal.t<int>`, or `unit => int`.

<a id="view-float-component" />

### View.Float

```rescript
<View.Float>{value}</View.Float>
<View.Float value={value} />
```

JSX float text primitive. `value` or `children` may be a raw float, `Signal.t<float>`, `MaybeSignal.t<float>`, or `unit => float`.

<a id="view-bool-component" />

### View.Bool

```rescript
<View.Bool>{value}</View.Bool>
<View.Bool value={value} />
```

JSX boolean text primitive. `value` or `children` may be a raw bool, `Signal.t<bool>`, `MaybeSignal.t<bool>`, or `unit => bool`.

## Example

```rescript
open Xote

let count = Signal.make(0)

let app = () => {
  <div>
    <View.Text> "Count: " </View.Text>
    <View.Int> {count} </View.Int>
    <button onClick={_ => Signal.update(count, n => n + 1)}>
      <View.Text> "Increment" </View.Text>
    </button>
  </div>
}

View.mountById(app(), "app")
```

## See Also

- [View guide](/docs/learn/components)
- [Signal API](/docs/api/signal)
