View API
Reference for UI nodes, JSX primitives, lists, attributes, and mounting.
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)56type 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 }78 let make: props<'item> => View.node9}
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 }78 let make: props<'item> => View.node9}
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 }78 let make: props => View.node9}
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 }78 let make: props<'value> => View.node9}
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 }67 let make: props<'value> => View.node8}
JSX primitive that re-renders a node from one static or reactive value.
View.Suspense
New in 7.21module Suspense: {2 type props = {3 fallback?: View.node,4 children?: View.node,5 }67 let make: props => View.node8}
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.21module Ref: {2 type t = View.elementRef34 let make: unit => t5 let fromCallback: (Dom.element => unit) => t6 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.21let 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.21let 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.21module Portal: {2 type props = {3 target?: Dom.element,4 children?: View.node,5 }67 let make: props => View.node8}
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.21module ErrorBoundary: {2 type props = {3 fallback: (exn, unit => unit) => View.node,4 onError?: exn => unit,5 children?: View.node,6 }78 let make: props => View.node9}
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 Xote23let count = Signal.make(0)45let 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}1415View.mountById(app(), "app")