This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-09-01
Channels
- # aleph (2)
- # announcements (3)
- # architecture (9)
- # babashka (56)
- # beginners (18)
- # calva (7)
- # catalyst (16)
- # cider (18)
- # cljfx (3)
- # cljs-dev (23)
- # clojure-europe (11)
- # clojure-hungary (1)
- # clojure-nl (2)
- # clojure-norway (24)
- # clojure-sweden (2)
- # clojurescript (37)
- # code-reviews (47)
- # datalog (3)
- # datomic (8)
- # emacs (11)
- # events (2)
- # graalvm (7)
- # gratitude (1)
- # hyperfiddle (12)
- # java (1)
- # jvm (46)
- # kaocha (3)
- # lsp (3)
- # malli (4)
- # matcher-combinators (1)
- # music (1)
- # nbb (1)
- # nrepl (4)
- # releases (1)
- # sci (15)
- # shadow-cljs (21)
- # slack-help (21)
- # tools-deps (17)
Hm... is there any conceptual difference between constructing of children with a map vs. a plain function call? Please consider the following snippet:
(defn component-a
[{context :fx/context}]
{:fx/type :label
:text (fx/sub-ctx context some-subscription-fn)})
(defn component-b
[context _something-else]
{:fx/type :label
:text (fx/sub-ctx context some-subscription-fn)})
(defn view
[{context :fx/context}]
{:fx/type :v-box
:children
[{:fx/type component-a}
(component-b context "something")]})
Above both component-a
and component-b
are children of view
and both receive the context via their arguments. They both seem to result in the same view tree, yet... is one approach better than the other? And why?There is small difference, and it probably doesn’t matter, because most of the time the difference is negligible.
component-a
is executed only when the props of the component change (and when its context dependencies change). Negligible con: slightly higher memory usage (cljfx tracks component-a
in its state). Negligible pro: slightly lower CPU usage because we don’t have to recompute the view of component-a
every time.
component-b
is executed every time view
is executed (i.e.: every time the props of view
change). Negligible con: in some cases you get a slightly higher CPU usage on recomputing the view tree unnecessarily. Negligible pro: slightly lower memory usage (cljfx doesn’t track component-b
in its state).
It might matter if you have component functions that are heavy to execute (e.g. they sort huge vectors of items) — you want to make sure those are re-evaluated only when necessary. Most probably, it doesn’t matter.
Aha. This makes sense indeed. Thanks for the insight. 👍