While a UIx component tree can be reloaded in a traditional way, by re-rendering from the root after reload, this resets component local state and re-runs hooks. It happens because the reloaded namespace will create new component functions, which in turn invalidate the UI tree when re-rendering after reload and cause the updated components to be re-mounted.
react-refresh makes it possible to reload components in a way that preserves local state and the state of hooks.
Make sure you have React DevTools installed for your browser
Install the package yarn add react-refresh --dev
Create a dev-only namespace that will be responsible for managning hot-reloading
(ns my.app.preload
(:require [uix.dev]))
;; initializes fast-refresh runtime
(uix.dev/init-fast-refresh!)
;; called by shadow-cljs after every reload
(defn ^:dev/after-load refresh []
;; performs components refresh
(uix.dev/refresh!))
:preloads
so that it's only loaded in dev;; shadow-cljs.edn
{:builds
{:build-id
{:devtools {:preloads [my.app.preload]}}}}
Make sure that your UI is not re-rendered from the root in a traditional way when using fast-refresh, otherwise fast-refresh won't work and the state will be reset.
Can you improve this documentation? These fine people already did:
Shaun Mahood & roman01laEdit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close