Skip to content

re-frame.adapter.uix

Use this namespace to run a re-frame2 app on UIx, a hooks-first React substrate. It provides the adapter you pass to rf/init! at boot, the use-sub and use-frame hooks components read and dispatch through, the frame-root and frame-provider components, and the client-root, render! and unmount! functions your entry namespace mounts the app through.

Pick it when your components are React function components written with defui and hooks, usually because the code around the app already is; for hiccup views, use re-frame.adapter.reagent or Fresco. It ships in the day8/re-frame2-uix artefact, which brings in com.pitch/uix.core.

(ns my-app.core
  (:require [re-frame.core :as rf]
            [re-frame.adapter.uix :as uix-adapter]
            [uix.core :refer [$ defui]]))
(rf/reg-event :counter/inc
  (fn [{:keys [db]} _]
    {:db (update db :counter/value (fnil inc 0))}))

(rf/reg-sub :counter/value
  (fn [db _] (:counter/value db 0)))

(defui counter-app []
  (let [n                  (uix-adapter/use-sub [:counter/value])
        {:keys [dispatch]} (uix-adapter/use-frame)]
    ($ :button {:on-click #(dispatch [:counter/inc])}
       "Clicked " n " times")))

(defonce app-root (uix-adapter/client-root))

(defn ^:dev/after-load mount! []
  (when-let [el (and (exists? js/document)
                     (js/document.getElementById "app"))]
    (uix-adapter/render! app-root
      ($ uix-adapter/frame-root {:id :rf/default}
         ($ counter-app))
      el)))

(defn run []
  (rf/init! uix-adapter/adapter)
  (mount!))

Everything else a UIx app calls is on re-frame.core, including the lifecycle functions init!, destroy-adapter! and current-adapter. Nothing is injected into a UIx component: it reads with use-sub and takes dispatch from use-frame. The dependency is one-way: this namespace requires re-frame.core, and core never requires it.

Components are plain defui functions that you mount by referring to their Var, the React idiom; the reg-view macro is Reagent-only. Register one with rf/reg-view* only when the call site holds an id rather than the Var (see Registry-keyed views). Use UIx covers choosing and switching substrate.

Adapter spec

adapter

  • Kind: var (map)
  • Signature:
    {:kind                      :rf.adapter/uix
     :make-state-container      …
     :read-container            …
     :replace-container!        …
     :subscribe-container       …
     :make-derived-value        …
     :render                    …
     :render-to-string          …
     :register-context-provider …
     :flush-render!             …
     :dispose-adapter!          …}
    
  • Description: The adapter map you pass to rf/init! to install UIx as the substrate. It implements the same adapter contract as the Reagent adapter; (:kind (rf/current-adapter)) reads :rf.adapter/uix.
  • Example:
    (rf/init! uix-adapter/adapter)   ;; install the substrate once, at boot
    

Hooks

use-sub

  • Kind: function (React hook)
  • Signature:
    (use-sub query-v)                  → current sub value
    (use-sub query-v {:frame target})  → current sub value, from target
    
  • Description: Returns the current value of the subscription query-v and re-renders the component when that value changes. Where subscribe returns a subscription, this returns its value. re-frame.fresco.native publishes a use-sub for React islands under Fresco that reads the same frame context; it has no opts form.
    • The 1-arity reads the frame from React context only: the nearest frame-provider (SCOPE) / frame-root (ENSURE) above the component. With no boundary above it, it raises :rf.error/no-frame-context; there is no fallback to :rf/default.
    • A with-frame scope around a render does not reach a hook, even when the render is synchronous (under act(), flushSync or a server render). React may render the same tree later, outside that scope, and the tree must resolve the same frame however the render was driven. re-frame.fresco.native's hooks follow the same rule.
    • To set the frame explicitly, wrap the component in a frame-provider, or use the opts form. The opts form, {:frame target}, reads that one value from target and bypasses context; target is a frame-id keyword or a live frame value, as for subscribe, and :frame is required: a missing or nil :frame raises :rf.error/no-frame-context. When a whole subtree shares a frame, scope it with frame-provider {:frame target} and use the 1-arity.
    • A query id with no registered subscription emits :rf.error/no-such-sub, and a frame that does not exist or has been destroyed emits :rf.error/frame-destroyed. In both cases the hook returns nil.
    • Read with use-sub, not rf/subscribe, in a defui body. Dereferencing a subscription in a UIx render does not re-render the component, and the cache reference each subscribe call takes is never released. The same holds for the :subscribe op in use-frame's map.
    • See Write a UIx view.
  • Example:
    (defui cart-total []
      (let [total (uix-adapter/use-sub [:cart/total])]
        ($ :span total)))
    
    ;; The opts form: compare one query across two frames in one component.
    (defui price-compare [{:keys [sym]}]
      (let [a (uix-adapter/use-sub [:quote/price sym] {:frame :tenant-a})
            b (uix-adapter/use-sub [:quote/price sym] {:frame :tenant-b})]
        ($ :span a " / " b)))
    

use-frame

  • Kind: function (React hook)
  • Signature:
    (use-frame) → {:frame … :dispatch … :dispatch-sync … :subscribe …}
    
  • Description: Returns the frame-locked ops map for the frame the component renders under — the same map (rf/capture-frame) returns. Destructure dispatch from it to dispatch from a UIx component; it is the hook form of the ops Reagent's reg-view injects.
    • It resolves the frame as use-sub does: from React context only, so a with-frame scope around a render does not reach it, and with no boundary above it raises :rf.error/no-frame-context.
    • It takes no options. For a named frame, call (rf/capture-frame frame-id) directly.
    • The map is the same object across re-renders while the resolved frame stays the same, so it is safe in effect deps and child props. A provider that switches frames re-renders the caller with a map for the new frame. Destroying the frame and creating another under the same id does not re-render the caller: its next render returns a map for the new frame, and until then the old map's ops emit :rf.error/frame-destroyed. The map belongs to the frame it was captured from, not to the id.
    • There is no hook that reads the frame context alone. (:frame (use-frame)) gives the frame from React context; (rf/current-frame-id) checks a with-frame binding first, so inside a with-frame around a render the two can differ.
    • See Why callbacks dispatch off the frame api.
  • Example:
    (defui inc-button []
      (let [{:keys [dispatch]} (uix-adapter/use-frame)]   ;; take dispatch during render
        ($ :button {:on-click #(dispatch [:counter/inc])} "+")))
    

Components

frame-root and frame-provider write the same React context as Reagent's rf/frame-root / rf/frame-provider and Fresco's h/frame-root / h/frame-provider, so the substrates nest: a UIx frame-provider can wrap a Reagent subtree, and the reverse. See Ensure a view's own frame.

frame-provider

  • Kind: component (UIx)
  • Signature:
    ($ uix-adapter/frame-provider {:frame :session} child…)
    
  • Description: Scopes a subtree to a frame that already exists; it creates, refreshes and destroys nothing. To create the frame if it is absent, use frame-root: roots ensure, providers scope.
    • Pass children after the props map, as for any UIx component; there is no :children prop key.
  • Errors:
    • :rf.error/frame-provider-frame-absent when the frame does not exist
    • :rf.error/no-frame-context on a nil :frame
    • :rf.error/bad-frame-provider-arg on a :frame that is neither a keyword nor a live frame value
    • :rf.error/frame-provider-given-id when given an :id, naming frame-root
  • Example:
    ($ uix-adapter/frame-provider {:frame :session}
       ($ dashboard))
    

frame-root

  • Kind: component (UIx)
  • Signature:
    ($ uix-adapter/frame-root {:id :session :images [session-image]} child…)
    
  • Description: Creates the named frame if it is absent, or reuses it without re-seeding if it is live, and provides it to the subtree. It takes the rf/make-frame options, including :images and :initial-events, and never destroys the frame on unmount.
    • :id is required and must be a keyword.
    • The frame is created and seeded in a client useLayoutEffect at commit, not during render. The first render emits no children; they render once the frame is live. A render React discards before commit creates and seeds nothing.
    • Re-mounting under the same :id (hot reload, React StrictMode's double mount in development) keeps the frame's state and does not re-run :initial-events.
    • Pass children after the props map.
  • Errors:
    • :rf.error/frame-root-missing-id when :id is missing, nil or not a keyword
    • :rf.error/frame-root-reconfigured when a mounted frame-root's :id or opts change; to switch frames, give it a React :key that changes
    • :rf.error/frame-root-given-frame on a :frame key, naming frame-provider
  • Example:
    ;; Create the counter's frame on first mount; reuse it on hot reload.
    ($ uix-adapter/frame-root {:id :rf/default}
       ($ counter-app))
    

The client root

A browser app needs one React root for the life of the page: created once, updated on every hot reload, released on teardown. client-root, render! and unmount! manage that root, so your entry namespace never creates one or builds a uix.dom root itself. Allocate the handle under a defonce and call render! from the ^:dev/after-load hook, as in the example at the top of this page, with run as the build's :init-fn. On a hot reload shadow-cljs calls mount! again: the defonce keeps the handle, render! updates the same root, and frame-root reuses the live frame without re-running :initial-events, so app-db survives the reload. Boot and mount an app has the whole recipe.

These are the same three functions as on re-frame.adapter.reagent, with the same behaviour, except that render! here takes a React element built with uix.core/$ rather than hiccup. The root is created through react-dom/client, so the app needs no com.pitch/uix.dom dependency to mount. The raw React root is never exposed, and rf/destroy-adapter! also releases it, exactly once.

client-root

  • Kind: function
  • Signature:
    (client-root) → handle
    
  • Description: Returns a new, inert client-root handle. It does no DOM work, so it is safe at namespace load under a defonce, in tests and on Node; the first render! through the handle creates (or hydrates) the React root.
    • The handle is opaque: pass it to render! and unmount! and nothing else.
  • Example:
    (defonce app-root (uix-adapter/client-root))   ;; inert until the first render!
    

render!

  • Kind: function
  • Signature:
    (render! handle element mount-point)      → nil
    (render! handle element mount-point opts) → nil
    
  • Description: Renders element, a React element built with uix.core/$, into the DOM element mount-point through handle: the first call creates the React root, and every later call updates it. Returns nil.
    • With {:hydrate? true} the first call hydrates the server-rendered markup already inside mount-point instead (see re-frame.ssr). Later calls never create a second root or hydrate a second time.
    • Because later calls update the same root, one call serves as both the boot path and the ^:dev/after-load hook. mount-point is read on the first call only.
    • opts takes :hydrate?, :on-recoverable-error and :identifier-prefix, read on the first call only. The last two are used only when hydrating. :identifier-prefix becomes React's identifierPrefix, which useId needs to match the server's, so pass the prefix the server rendered under. :on-recoverable-error becomes the root's onRecoverableError, and on a hydrating root the adapter wraps it in every build: each mismatch React recovers from during hydration is first reported as an always-on :rf.ssr/hydration-mismatch error record that reaches your error sinks (:where and :recovery :warned-and-replaced; no message, markup or state), and in development builds also as the warning trace of the same id, which carries React's message under :error. Then the wrapper calls your callback, or React's default report when you gave none. After the hydration commit, recoverable errors are reported as neither and go straight to your callback, or to React's default report.
    • Hiccup or other CLJS data in the element position (a vector, seq or map) raises :rf.error/hiccup-on-element-render-slot, on the first render and every later one. Hiccup mounts only on the Reagent adapters.
    • After unmount!, or after rf/destroy-adapter! has released the root, the next render! mounts afresh.
    • Call rf/init! before the first render!. render! does not check for an adapter, but the first frame or subscription the tree creates raises :rf.error/no-adapter-installed, or :rf.error/adapter-disposed after rf/destroy-adapter!. Install an adapter again before rendering afresh.
  • Example:
    (uix-adapter/render! app-root
      ($ uix-adapter/frame-root {:id :rf/default} ($ counter-app)) el)
    ;; Later calls update the same root and reuse its frame.
    
    ;; Alternative boot for an SSR page: hydrate on this handle's FIRST render.
    (defonce hydrated-root (uix-adapter/client-root))
    (uix-adapter/render! hydrated-root
      ($ uix-adapter/frame-provider {:frame :rf/default} ($ counter-app))
      el {:hydrate? true})
    ;; Hydration has already installed :rf/default; later renders update this root.
    

unmount!

  • Kind: function
  • Signature:
    (unmount! handle) → nil
    
  • Description: Unmounts the React root handle holds and returns the handle to inert, so a later render! mounts afresh. Returns nil.
    • Unmounting releases the view tree; it does not destroy the frames used by that tree. A frame-root reuses its live frame on remount. If the app owns a frame that should end here, call rf/destroy-frame! separately.
    • Idempotent: a second call, or a call after rf/destroy-adapter! has released the root, does nothing.
  • Example:
    (uix-adapter/unmount! app-root)   ;; releases the root; a repeat call is a no-op
    

Registry-keyed views

Mounting a component by its Var is the idiom, and most UIx code needs nothing else. Use the registry when the call site cannot name the Var: a view chosen at runtime, a component used across a module boundary, or a library that ships ids rather than symbols.

rf/reg-view* takes an id and a component; rf/view returns a UIx component head, which you mount with $ like any other:

(defui cart-row [{:keys [item]}]
  (let [count (uix-adapter/use-sub [:cart/count])]
    ($ :tr
       ($ :td (:name item))
       ($ :td count))))

(rf/reg-view* ::cart-row cart-row)

;; …anywhere, including a module that cannot see the Var:
($ (rf/view ::cart-row) {:item item})

The head behaves like the registered component itself:

  • Hand it to $ as the component type. Do not call it inside a host component of your own: the hooks and the instance lifetime would then belong to your host rather than to the registered view.
  • Props and children arrive unchanged. The head is marked as a UIx component, so $ passes the original ClojureScript map through UIx's argv channel: namespaced keywords stay keywords, nested maps stay maps, and trailing $ children reach the component as :children.
  • It does not matter whether rf/reg-view* runs before or after rf/init!: rf/view resolves the head against the adapter installed when you look it up. The usual order is to register at namespace load and call init! afterwards, as Boot and mount an app does. Repeat lookups return the same object, so React reconciles it as one component type instead of remounting.

rf/view returns nil for an unregistered id.

In development builds, a registered view's root element gets data-rf2-source-coord and data-rf-view attributes, which Xray and re-frame2-pair use for click-to-source and view lookup. A view whose root is not a DOM element (a Fragment or another component) gets neither, and logs a one-time console.warn naming the view. Production builds remove all of this, so it costs no shipped bytes. See Observability.

Controlled inputs and the caret

A UIx :input with a :value and an :on-change is a plain React controlled input, whatever else is in the bundle. Left unset, UIx chooses between React's implementation and a port of Reagent's controlled-input workaround by checking whether Reagent is on the classpath, so adding the Reagent adapter beside UIx would change how the UIx app's inputs behave. Requiring this namespace sets uix.compiler.input/*use-reagent-input-enabled?* to false at load, which pins React's implementation.

When your handler rejects or rewrites a keystroke, React restores the field inside the event: the rejected character is gone before dispatchEvent returns, with nothing re-rendered. Writing value moves the caret to the end of the field, though. Type z into "12345" with the caret at position 2, have the handler reject it, and you get "12345" with the caret at 5. This is React's own controlled-input caret jump, and it happens on every write React makes. A handler that accepts the keystroke unchanged never triggers a write and never moves the caret.

The port makes the element uncontrolled and restores both value and caret itself, but one animation frame later, off Reagent's requestAnimationFrame queue, never inside the event. So React's implementation restores the value in time but moves the caret, and the port keeps the caret but restores a frame late. The adapter pins React's path because inputs that behave differently depending on what else is in the bundle are the worse problem.

To use the port instead, set the var yourself after requiring the adapter and before you render:

(:require [re-frame.adapter.uix :as uix-adapter]
          [uix.compiler.input])

(set! uix.compiler.input/*use-reagent-input-enabled?* true)

true selects UIx's port, false selects React's implementation, and nil restores UIx's classpath check. The port uses reagent.impl.batching, so it needs Reagent on the classpath and is not an option for a UIx-only bundle.

Server-side rendering

set-hiccup-emitter!

  • Kind: function
  • Signature:
    (set-hiccup-emitter! f)
    
  • Description: Installs the function the adapter's render-to-string uses to turn a render tree into HTML. Requiring re-frame.ssr installs it for you, so you rarely call this directly.
    • f takes the render tree and an opts map, and returns an HTML string.
    • Last call wins; pass nil to reset.
    • With no emitter installed, render-to-string raises :rf.error/no-hiccup-emitter-bound.
    • rf/destroy-adapter! clears the installed emitter. The next rf/init! re-installs re-frame.ssr's emitter when that namespace is loaded, and otherwise leaves the slot empty. An emitter you install before rf/init! is kept, so install a custom emitter again after re-initialising.
    • The Reagent adapter has the same function.
  • Example:
    (uix-adapter/set-hiccup-emitter! (fn [tree _opts] (str tree)))
    (uix-adapter/set-hiccup-emitter! nil)   ;; reset
    

See also

  • re-frame.adapter.reagent — the Reagent adapter, which the Core guide uses, with the same client-root functions.
  • re-frame.fresco.native — the use-sub / use-frame hooks for React islands under Fresco, reading the same frame context.
  • Use UIx — worked examples and the full substrate decision.
  • Adapter in the glossary.