This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-08-16
Channels
- # announcements (25)
- # babashka (15)
- # beginners (14)
- # calva (60)
- # circleci (1)
- # clerk (1)
- # clj-kondo (13)
- # cljdoc (7)
- # clojure (45)
- # clojure-austin (1)
- # clojure-bay-area (1)
- # clojure-brasil (4)
- # clojure-dev (9)
- # clojure-europe (24)
- # clojure-norway (105)
- # clojure-uk (2)
- # clojurescript (6)
- # conjure (1)
- # core-typed (4)
- # cursive (4)
- # datalevin (1)
- # datomic (25)
- # emacs (31)
- # fulcro (3)
- # humbleui (10)
- # hyperfiddle (19)
- # jobs (2)
- # luminus (3)
- # malli (13)
- # nbb (5)
- # off-topic (16)
- # polylith (2)
- # portal (7)
- # releases (2)
- # shadow-cljs (5)
- # sql (8)
Hello 👋 I’m playing around with HumbleUI and have a basic question. How do I make the UI “reactive” to changes in state? I assumed this is what the ui/dynamic
macro helps with but I’m having trouble. For example, in the code below, I would have thought that evaluating (reset! *message "Goodbye")
would change the message displayed in the window, but I’m not seeing that - the message stays as “Hello”
(ns ui-test
(:require [io.github.humbleui.ui :as ui]))
(def *message (atom "Hello"))
(def ui
(ui/default-theme {}
(ui/center
(ui/dynamic _ [message @*message]
(ui/label message)))))
(ui/start-app!
(ui/window
{:title "Humble 🐝 UI"}
ui))
(comment
(reset! *message "Goodbye"))
You should call window/request-frame
after you change your atom. Dynamic will pick up the change but it won't trigger re-render by itself
Ah okay. Is that what the examples in dev/examples are using?
Most of the examples don’t do that because they have e.g. button and when you press a button it needs to repaint itself. So there’s no need to request repaint explicitly. But it’s just a coincidence, don’t rely on it
The best way is to add-watch to your state and call request-frame. See e.g. examples.treemap
Okay will look into that later after work. Thank you 🙏 I watched your Wordle video on YouTube and was curious how the UI seemed to update when you updated any aspect of the state (like the currently :typing word or :guesses, etc)
maybe that’s just the coincidental repainting you mentioned?
Okay got it working! In case it helps anyone else, here’s a minimal example:
(ns ui-test
(:require [io.github.humbleui.ui :as ui]
[io.github.humbleui.window :as window]))
(def *message (atom "Hello!"))
(def example-ui
(ui/default-theme {}
(ui/center
(ui/dynamic _ [message @*message]
(ui/label message)))))
(ui/start-app!
(let [window (ui/window
{:title "Humble 🐝 UI"}
example-ui)]
(add-watch *message :message
(fn [_ _ _ _]
(window/request-frame window)))
window))
(comment
; Evaluate the form below and UI will update
(reset! *message "Goodbye"))