Skip to content

Rendering

Show changing text, set attributes, handle events, and render conditions and lists.

docs/learn/rendering.md
moduleXote.Viewboundarysrc/View.resientryxote · xote/viewversion7.2.0
Raw MarkdownAPI reference

This page covers what goes inside a component's JSX: text, attributes, events, conditions and lists. Examples use @xote.component; the last section shows the same things without it.

Show values

Put a value in braces to show it. Strings and numbers are shown as text. A Signal.get stays live: that text updates when the signal changes, and nothing around it does.

1@xote.component
2let make = () =>
3 <div>
4 {"Count: "}
5 {Signal.get(count)}
6 <p> {`Hello, ${Signal.get(name)}!`} </p>
7 </div>

Any expression that reads a signal works, including template strings and function calls. Computeds read the same way: {Signal.get(total)}.

Set attributes

Attributes take plain values or expressions. If the expression reads a signal, the attribute updates on its own.

1<button
2 class={Signal.get(isActive) ? "btn active" : "btn"}
3 disabled={Signal.get(count) > 9}
4 onClick={_ => Signal.update(isActive, active => !active)}>
5 {"Toggle"}
6</button>

A few things to know:

  • Use class, not className.
  • type is a ReScript keyword, so write type_: <input type_="text" />.
  • You can pass a signal straight in when no formatting is needed: <div class={theme}>.
  • style takes a string: style={`width: ${Int.toString(Signal.get(progress))}%`}.
  • For an attribute without a typed prop, use attrs: attrs=[("aria-live", "polite"), ("data-state", "idle")].

The Styling guide has more on classes and styles.

Handle events

Event props like onClick, onInput, onChange and onSubmit take a function that receives the DOM event. Handlers usually update a signal:

1<button onClick={_ => Signal.update(count, n => n + 1)}> {"Add one"} </button>

To read what someone typed, read the event's target. The Forms and inputs guide shows the small binding for that, plus checkboxes, selects and submits.

Show content conditionally

Use if or switch right in the JSX. When the signal in the condition changes, Xote swaps that branch and leaves the rest of the component alone.

1type status = Loading | Ready(string) | Failed
2
3@xote.component
4let make = () =>
5 <div>
6 {switch Signal.get(status) {
7 | Loading => "Loading…"
8 | Ready(message) => <strong> {message} </strong>
9 | Failed => <p class="error"> {"Something went wrong."} </p>
10 }}
11 </div>

To render nothing in a branch, use View.null().

There are also components for this. View.Show renders its children when a condition holds and fallback otherwise; View.Maybe renders an option value or a fallback:

1<View.Show when_={MaybeSignal.reactive(isReady)} fallback={<p> {"Loading…"} </p>}>
2 <p> {"Ready"} </p>
3</View.Show>
4
5<View.Maybe
6 value={MaybeSignal.reactive(selected)}
7 fallback={<p> {"Nothing selected"} </p>}
8 render={item => <p> {"Selected: "} {item} </p>}
9/>

Render lists

Use View.For with an array signal. Wrap the signal in MaybeSignal.reactive (or a fixed array in MaybeSignal.static).

1let fruits = Signal.make(["Apple", "Banana", "Cherry"])
2
3<ul>
4 <View.For each={MaybeSignal.reactive(fruits)} render={fruit => <li> {fruit} </li>} />
5</ul>

When items can be added, removed or reordered, give each one a stable key with by. Xote then moves existing rows instead of rebuilding them, so their DOM and local state survive:

1<ul>
2 <View.For
3 each={MaybeSignal.reactive(todos)}
4 by={todo => todo.id}
5 render={todo => <li> {todo.text} </li>}
6 />
7</ul>

Use IDs or slugs as keys, never array positions. If a row has values that change on their own, like a todo's done flag, give that value its own signal so only that row updates. The todo example does this.

Group a reactive region

Sometimes a block depends on several signals in a way that doesn't map to one attribute or one condition. View.tracked rebuilds a block whenever any signal it reads changes:

1{View.tracked(() =>
2 Signal.get(isReady) ? <p> {"Hello, "} {Signal.get(name)} </p> : <p> {"Please log in"} </p>
3)}

It replaces the whole block on each change, so focus or text selection inside it is lost. Keep it small, and prefer the plain if/switch above, which only rebuilds when the condition changes.

Without the JSX transform

If you leave out ppx-flags and use @jsx.component, reads in JSX are not made live for you. Wrap reactive attributes in a function, and use the value components for text:

1@jsx.component
2let make = () =>
3 <div class={() => Signal.get(isActive) ? "on" : "off"}>
4 <View.Text> "Count: " </View.Text>
5 <View.Int> {count} </View.Int>
6 <View.Text> {() => `Doubled: ${Int.toString(Signal.get(count) * 2)}`} </View.Text>
7 </div>

View.Text, View.Int, View.Float and View.Bool take a value, a signal, or a function. These work under @xote.component too, if you want the stricter int or float typing.

Common mistakes

  • Computing a value in the component body to show it. The body runs once. Read signals in the JSX, or make a computed.
  • Keying list rows by index. Keys must follow the item, not its position.
  • Using View.tracked for a whole page. Every change rebuilds all of it. Use it for small blocks only.

Summary

Rendering in Xote builds DOM once and then updates individual reactive leaves (one attribute or one text node) and reactive regions (a branch of an if/switch, a list, a View.tracked block). Under @xote.component, the PPX decides at compile time which JSX positions become reactive by looking for signal reads in them. The rule that matters most: reactivity follows the expression in JSX position; a value computed outside JSX, or passed as a user-component prop, is read once.

Availability

ItemModule / entryStable sinceChanged in 7.2.0PPX required
Decomposition of an annotated component's returned JSX; bare {…} children via View.childPPX + xote7.1.0—yes
Decomposition of JSX anywhere in an annotated file (helpers, let-bound JSX, render callbacks); if/switch regions track only their condition; local-helper and alias detection; View.probe warningsPPX7.2.0 (new)—yes
View.For, View.KeyedFor, View.Show, View.Maybe, View.Value, View.Text/Int/Float/Bool, View.tracked, View.renderxote · xote/view7.1.0—no
attrs escape hatch, removable (optional) attributesXoteJSX7.1.0—no
ref prop on elementsXoteJSX7.2.0 (new)—no

Rules

What becomes reactive under @xote.component

  1. An attribute value on an intrinsic element (<div>, <input>, …) that reads a signal is wrapped in a thunk and becomes a reactive attribute leaf (View.computedAttr). An attribute value that reads no signal stays static.
  2. A bare child {expr} is wrapped in View.child. If expr reads a signal eagerly, it is thunked first and becomes a reactive text leaf; otherwise it is coerced once.
  3. The child of <View.Text>, <View.Int>, <View.Float>, <View.Bool> that reads a signal is thunked into a reactive text leaf. These are recognised only when written qualified (View.Text), not after open View or module V = View.
  4. An if/switch child whose condition or scrutinee (including when guards) reads a signal becomes a reactive region (View.tracked) that tracks only the condition; leaves inside each branch stay independent reactive leaves (7.2.0). When the condition's value changes, the selected branch is rebuilt from scratch (new DOM, new component instances); when only a leaf inside it changes, the branch DOM is kept.
  5. An if/switch whose condition reads no signal is not a reactive region; its branches are still decomposed.
  6. A user-component prop (<Card label={…} />) is never thunked: it is a read (untracked) at the call site. Exceptions: a prop whose value is JSX, or a function returning JSX (such as render={item => <li>…</li>}), is decomposed like other JSX.
  7. Event handler props (onClick, …), the attrs array and the data object are never thunked or probed. Entries of attrs and data carry their own reactivity: a signal or a thunk there is reactive; Signal.get(x) there is a one-shot value.
  8. Signal-read detection is syntactic. Detected: Signal.get(x), x->Signal.get, MaybeSignal.get, Prop.get, let/module/open aliases of those, and local helpers whose body reads a signal eagerly (including helpers in a module defined in the same file). Not detected: reads behind a call into another module, or reached through a data structure. Signal.peek is never treated as a read.
  9. A read the PPX cannot see compiles to a one-shot value. In development builds View.probe reports it once at runtime: [Xote] File.res:L:C: this value reads a signal through a call @xote.component cannot see …. The fix is always to wrap the value in a thunk () => … (never double-wrapped).
  10. Binding a read to a name hoists it: let label = Signal.get(x) is one-shot. Bind a thunk instead (let label = () => …, then {label()} or {label}).

View.child coercions (bare children)

  1. A node passes through unchanged. A Signal.t or a thunk becomes reactive text. A string, int, float or bool becomes static text. An array becomes a fragment (nodes rendered as nodes, scalars as text). null/undefined render nothing. Any other object (record, variant, dict) renders [object Object] with a development warning; it is NOT a compile error under the PPX.

Attributes

  1. Typed attribute props accept a raw value, a Signal.t, a thunk unit => 'a or a MaybeSignal.t. A signal or thunk makes the attribute reactive without the PPX too.
  2. Use class (not className), type_ for type, for_ for for. Hyphenated names are not props: data-x={…} or aria-controls={…} is a compile error ("The field data-x does not belong to type Xote.XoteJSX.Elements.props"). Use data or attrs.
  3. style is a string (CSS text), not an object. An object compiles (the prop is untyped) but renders style="[object Object]".
  4. Presence attributes checked, disabled, required, readOnly (→ readonly), multiple, draggable, hidden, contentEditable, spellcheck, autofocus take a bool: true writes the attribute with an empty value, false removes it.
  5. ariaHidden, ariaExpanded, ariaSelected take a bool and write the literal string "true" or "false" (enumerated ARIA attributes are never removed by false). ariaLabel takes a string.
  6. maxLength, minLength, rows, cols, tabIndex are typed int and are static only: a signal read there is a compile error under the PPX ("This has type: 'a => 'b … expected to have type: int"). For a reactive value, use attrs with View.computedAttr("tabindex", () => …).
  7. value and checked are written as DOM properties, so setting the bound signal changes what the control shows (including clearing it).
  8. attrs is array<(string, 'attrs)>: all entries in one array MUST have the same type. Mix static and reactive entries by building every entry with View.attr, View.signalAttr, View.computedAttr, View.optionalAttr, View.optionalSignalAttr or View.optionalComputedAttr (all return (string, View.attrValue)). attrs entries are applied after typed props; for a duplicate key the last entry wins.
  9. An optional attribute value of None removes the attribute (View.optionalAttr, View.optionalSignalAttr, View.optionalComputedAttr, or None in attrs).
  10. data is an Obj.t (data={Obj.magic({"theme": theme})}); each key k is written as data-k. Values follow rule 12.

Events

  1. Event props take Dom.event => unit and are attached once (never reactive, never re-bound). Supported props: onClick, onInput, onChange, onSubmit, onFocus, onBlur, onKeyDown, onKeyUp, onMouseEnter, onMouseLeave, onMouseDown, onMouseMove, onMouseUp, onContextMenu, onPointerDown, onPointerMove, onPointerUp, onPointerCancel, onPointerEnter, onPointerLeave, onPointerOver, onPointerOut, onGotPointerCapture, onLostPointerCapture, onDrag, onDragStart, onDragEnd, onDragOver, onDragEnter, onDragLeave, onDrop. Other events: View.element(tag, ~events=[("name", handler)], ()).
  2. onChange is the native change event (fires on commit for text inputs); for per-keystroke updates use onInput. Xote has no synthetic event system and no helper to read event.target.value: declare a binding, for example @get external target: Dom.event => {..} = "target" and read target(event)["value"].
  3. Handlers run outside any tracking scope; a Signal.get inside a handler subscribes nothing.

Lists

  1. Use View.For (key optional) or View.KeyedFor (key required) with each: MaybeSignal.t<array<'item>>: MaybeSignal.reactive(signal) or MaybeSignal.static(array). each is a typed component prop, so passing a bare signal is a type error.
  2. Without by, every change of the array re-renders every row (no DOM reuse; focus and input state are lost). With by, rows are reconciled by key: a row is kept (moved if needed) only when both its key and its item are the same (===); an item replaced by a new value under the same key gets a newly built row.
  3. Keys MUST be stable and unique per item (ids, slugs). Array indexes as keys are wrong for insertions, removals and reordering.
  4. For per-row state that changes (for example a done flag), store a Signal.t in the item and read it in the row; replacing the whole item record rebuilds the row.
  5. {Signal.get(items)->Array.map(item => <li>…</li>)} compiles under the PPX but is one reactive region re-rendered wholesale on every change. Use View.For with by.

Other regions

  1. View.Show (when_: MaybeSignal.t<bool>, children, optional fallback), View.Maybe (value: MaybeSignal.t<option<'v>>, render, optional fallback) and View.Value (value: MaybeSignal.t<'v>, render) rebuild their content whenever their value changes.
  2. View.tracked(() => node) re-evaluates the whole body and replaces its children wholesale whenever any signal read in it changes; dependencies are re-discovered on every run. Local DOM state inside (focus, selection, input text) is lost on each update. Keep it small.
  3. View.null() (alias View.empty()) renders nothing but keeps a position (an empty text node) so a region can later replace it.

API

NameSignatureBehaviour
View.child'a => nodeCoerces a bare child (rule 11); emitted by the PPX.
View.tracked(unit => node) => nodeAuto-tracked region, rebuilt wholesale on change.
View.Forprops {each: MaybeSignal.t<array<'item>>, by?: 'item => string, render: 'item => node}List; keyed when by is given.
View.KeyedForprops {each, by: 'item => string, render}List with a mandatory key.
View.Showprops {when_: MaybeSignal.t<bool>, children?: node, fallback?: node}Boolean branch.
View.Maybeprops {value: MaybeSignal.t<option<'v>>, render: 'v => node, fallback?: node}Option branch.
View.Valueprops {value: MaybeSignal.t<'v>, render: 'v => node}Re-renders render(value) on change.
View.render(MaybeSignal.t<'a>, 'a => node) => nodeFunction form of View.Value.
View.Text / Int / Float / Boolprops {value?, children?} (untyped)Value text; reactive for a signal or thunk.
View.text / int / float / boolstring => node etc.Static text nodes.
View.signalText / signalInt / signalFloat(unit => string) => node etc.Reactive text from a thunk.
View.null / View.emptyunit => nodeRenders nothing, keeps a position.
View.attr / signalAttr / computedAttr(string, string) => (string, attrValue); (string, Signal.t<string>) => …; (string, unit => string) => …Attribute entries for attrs and View.element.
View.optionalAttr / optionalSignalAttr / optionalComputedAttr(string, option<string>) => … etc.None removes the attribute.
View.element(string, ~attrs=?, ~events=?, ~children=?, ~ref=?, unit) => nodeAny tag, any event name.

Patterns

Reactive text and attributes. Use when: displaying signal values.

1@xote.component
2let make = () =>
3 <div>
4 {"Count: "}
5 {Signal.get(count)}
6 <p> {`Hello, ${Signal.get(name)}`} </p>
7 <button
8 class={Signal.get(isOpen) ? "btn open" : "btn"}
9 disabled={Signal.get(count) > 9}
10 ariaExpanded={Signal.get(isOpen)}
11 onClick={_ => Signal.update(isOpen, v => !v)}>
12 {"Toggle"}
13 </button>
14 <div class={theme} />
15 <div style={`width: ${Int.toString(Signal.get(progress))}%`} />
16 </div>

Attributes without a typed prop, mixed static and reactive. Use when: ARIA/data attributes, removable attributes.

1let error: Signal.t<option<string>> = Signal.make(None)
2
3<input
4 attrs=[
5 View.attr("aria-describedby", "hint"),
6 View.computedAttr("data-theme", () => Signal.get(theme)),
7 View.optionalSignalAttr("aria-invalid", error),
8 ]
9/>

Controlled text input. Use when: binding an input to a signal.

1@get external target: Dom.event => {..} = "target"
2
3@xote.component
4let make = () =>
5 <input value={Signal.get(draft)} onInput={event => Signal.set(draft, target(event)["value"])} />

Variant-driven branch. Use when: showing one of several states.

1type status = Loading | Ready(string) | Failed
2
3@xote.component
4let make = () =>
5 <div>
6 {switch Signal.get(status) {
7 | Loading => "Loading…"
8 | Ready(message) => <strong> {message} </strong>
9 | Failed => <p> {"Failed"} </p>
10 }}
11 </div>

Keyed list with per-row state. Use when: rows can be added, removed, reordered or edited.

1type todo = {id: string, text: string, done: Signal.t<bool>}
2let todos: Signal.t<array<todo>> = Signal.make([])
3
4@xote.component
5let make = () =>
6 <ul>
7 <View.KeyedFor
8 each={MaybeSignal.reactive(todos)}
9 by={todo => todo.id}
10 render={todo => <li class={Signal.get(todo.done) ? "done" : ""}> {todo.text} </li>}
11 />
12 </ul>

Component primitives. Use when: you prefer components to inline if/switch, or need a MaybeSignal input.

1<View.Show when_={MaybeSignal.reactive(isOpen)} fallback={<p> {"Closed"} </p>}>
2 <p> {"Open"} </p>
3</View.Show>
4<View.Maybe value={MaybeSignal.reactive(selected)} fallback={<p> {"None"} </p>} render={item => <p> {item} </p>} />
5<View.Value value={MaybeSignal.reactive(count)} render={n => <p> {Int.toString(n)} </p>} />

Without the PPX (@jsx.component, no ppx-flags). Use when: a project cannot use the PPX.

1module Counter = {
2 @jsx.component
3 let make = () =>
4 <button
5 class={() => Signal.get(count) > 0 ? "on" : "off"}
6 onClick={_ => Signal.update(count, n => n + 1)}>
7 <View.Int> {count} </View.Int>
8 </button>
9}

Anti-patterns

Hyphenated attribute as a prop.

1<div data-state="idle" />

Symptom: compile error "The field data-state does not belong to type Xote.XoteJSX.Elements.props". Fix: attrs=[("data-state", "idle")] or data={Obj.magic({"state": "idle"})}.

Mixing entry types in attrs.

1attrs=[("aria-describedby", "hint"), ("data-theme", () => Signal.get(theme))]

Symptom: compile error "This has type: 'a => 'b … expected to have type: string". Fix: build every entry with View.attr / View.computedAttr / … so they share View.attrValue.

Reactive value in an int prop.

1<textarea rows={Signal.get(lines)} />

Symptom: compile error under the PPX (the thunk is not an int). Fix: attrs=[View.computedAttr("rows", () => Int.toString(Signal.get(lines)))].

Unkeyed list for interactive rows.

1<View.For each={MaybeSignal.reactive(todos)} render={todo => <li> <input /> </li>} />

Symptom: typing in a row's input loses focus/text whenever the array changes (every row is rebuilt). Fix: add by={todo => todo.id} (or use View.KeyedFor).

Editing a row by replacing its record.

1Signal.update(todos, list => list->Array.map(t => t.id == id ? {...t, done: !t.done} : t))

Symptom: with done: bool, the edited row is rebuilt (new DOM, lost focus/transition). Fix: make the changing field a signal (done: Signal.t<bool>) and update it directly: Signal.update(todo.done, d => !d).

Rendering a variant or record directly.

1{Signal.get(status)}

Symptom: renders [object Object] (development warning), no compile error under the PPX. Fix: switch over the variant and return strings or nodes.

onChange for live text.

Symptom: the bound signal updates only when the input loses focus. Fix: use onInput for text inputs; onChange for checkboxes, selects and file inputs.

Edge cases

  • An if/switch branch that contains a user component whose props read signals subscribes the branch to those signals too (props are evaluated in the branch's scope): a change to them rebuilds the branch. Pass signals instead of values to keep the branch subscribed to its condition only.
  • View.tracked blocks and Show/Maybe/Value rebuild their subtree; component instances inside are created again and lose local state.
  • A branch swap does no diffing between old and new branch DOM.
  • View.child coerces at runtime: an array of strings renders one text node per element.
  • ref (7.2.0) is a prop of intrinsic elements only (View.Ref.t), see /docs/learn/lifecycle.
  • The PPX is per file; see /docs/learn/components rule 8.

SSR and hydration

  • Server rendering evaluates every reactive leaf and region once and serialises the result with hydration markers (comments such as <!--lc-->). View.tracked and other regions emit markers so the client can attach to them.
  • Hydration attaches leaves and regions to the existing DOM; the first client evaluation MUST match the server output.
  • Event handlers are attached on hydration; nothing runs on the server for them.

Verify

  • Compile errors listed under Anti-patterns map one-to-one to rules 13, 17 and 19.
  • jsdom test: mount, change a signal, check that (1) the expected attribute/text changed and (2) the surrounding element is the same object (===) as before. For a branch, change only an inner leaf's signal and check the branch's element is unchanged; change the condition and check it is replaced. For a keyed list, reorder the array and check row elements are the same objects.
  • In development, watch the console for [Xote] … reads a signal through a call @xote.component cannot see warnings; each one is a one-shot value that should be a thunk.
  • /docs/learn/components — component definition, props, per-file PPX scope.
  • /docs/learn/lifecycle — refs, onMount, portals, error boundaries.
  • /docs/guides/forms — inputs, checkboxes, selects, submit.
  • /docs/guides/styling — classes, style, data-* theming.
  • /docs/api/view — full View reference.

API reference

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

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

View types

1type attrValue =
2 | Static(string)
3 | SignalValue(Signal.t<string>)
4 | Compute(unit => string)
5
6type node

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

View.attr

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

Create a static attribute entry.

View.signalAttr

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

Create a reactive attribute from a string signal.

View.computedAttr

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

Create a reactive attribute from a compute function.

View.Attr

1View.Attr.string: (string, string) => (string, View.attrValue)
2View.Attr.signal: (string, Signal.t<string>) => (string, View.attrValue)
3View.Attr.compute: (string, unit => string) => (string, View.attrValue)

Namespaced aliases for View.attr, View.signalAttr, and View.computedAttr.

View.text

1let text: string => View.node

Create a static text node.

View.int

1let int: int => View.node

Create a static integer text node.

View.float

1let float: float => View.node

Create a static float text node.

View.bool

1let bool: bool => View.node

Create a static boolean text node.

View.signalText

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

Create a reactive text node from a compute function.

View.signalInt

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

Create a reactive integer text node from a compute function.

View.signalFloat

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

Create a reactive float text node from a compute function.

View.fragment

1let fragment: array<View.node> => View.node

Group several nodes without adding a wrapper element.

View.signalFragment

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

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

View.tracked

1let 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 or View.eachWithKey for lists.

View.each

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

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

View.list

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

Alias for View.each.

View.eachWithKey

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

Render a reactive array with keyed DOM reconciliation.

View.keyedList

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

Alias for View.eachWithKey.

View.For

1module For: {
2 type props<'item> = {
3 each: MaybeSignal.t<array<'item>>,
4 by?: 'item => string,
5 render: 'item => View.node,
6 }
7
8 let make: props<'item> => View.node
9}

JSX list primitive. Pass by to enable keyed reconciliation.

View.KeyedFor

1module KeyedFor: {
2 type props<'item> = {
3 each: MaybeSignal.t<array<'item>>,
4 by: 'item => string,
5 render: 'item => View.node,
6 }
7
8 let make: props<'item> => View.node
9}

JSX list primitive that always requires a key function.

View.Show

1module Show: {
2 type props = {
3 when_: MaybeSignal.t<bool>,
4 children?: View.node,
5 fallback?: View.node,
6 }
7
8 let make: props => View.node
9}

JSX conditional primitive for boolean branches.

View.Maybe

1module Maybe: {
2 type props<'value> = {
3 value: MaybeSignal.t<option<'value>>,
4 render: 'value => View.node,
5 fallback?: View.node,
6 }
7
8 let make: props<'value> => View.node
9}

JSX conditional primitive for option values.

View.Value

1module Value: {
2 type props<'value> = {
3 value: MaybeSignal.t<'value>,
4 render: 'value => View.node,
5 }
6
7 let make: props<'value> => View.node
8}

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

View.Suspense

New in 7.2
1module Suspense: {
2 type props = {
3 fallback?: View.node,
4 children?: View.node,
5 }
6
7 let make: props => View.node
8}

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.

View.Ref

New in 7.2
1module Ref: {
2 type t = View.elementRef
3
4 let make: unit => t
5 let fromCallback: (Dom.element => unit) => t
6 let get: t => option<Dom.element>
7 let peek: t => option<Dom.element>
8}

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.

View.onMount

New in 7.2
1let 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.

View.onCleanup

New in 7.2
1let onCleanup: (unit => unit) => unit

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

View.Portal

New in 7.2
1module Portal: {
2 type props = {
3 target?: Dom.element,
4 children?: View.node,
5 }
6
7 let make: props => View.node
8}

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.

View.ErrorBoundary

New in 7.2
1module ErrorBoundary: {
2 type props = {
3 fallback: (exn, unit => unit) => View.node,
4 onError?: exn => unit,
5 children?: View.node,
6 }
7
8 let make: props => View.node
9}

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.

View.element

1let element: (
2 string,
3 ~attrs: array<(string, View.attrValue)>=?,
4 ~events: array<(string, Dom.event => unit)>=?,
5 ~children: array<View.node>=?,
6 ~ref: View.elementRef=?,
7 unit,
8) => View.node

Create an element node for any tag.

View.null

1let null: unit => View.node

Create an empty placeholder text node.

View.empty

1let empty: unit => View.node

Alias for View.null.

View.mount

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

Render a node into a DOM element.

View.mountById

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

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

View.Text

1<View.Text>{value}</View.Text>
2<View.Text value={value} />

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

View.Int

1<View.Int>{value}</View.Int>
2<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.

View.Float

1<View.Float>{value}</View.Float>
2<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.

View.Bool

1<View.Bool>{value}</View.Bool>
2<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

1open Xote
2
3let count = Signal.make(0)
4
5let app = () => {
6 <div>
7 <View.Text> "Count: " </View.Text>
8 <View.Int> {count} </View.Int>
9 <button onClick={_ => Signal.update(count, n => n + 1)}>
10 <View.Text> "Increment" </View.Text>
11 </button>
12 </div>
13}
14
15View.mountById(app(), "app")

See Also