This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-03-16
Channels
- # babashka (48)
- # beginners (72)
- # calva (65)
- # cider (10)
- # clerk (11)
- # clj-kondo (14)
- # clojure (85)
- # clojure-austin (11)
- # clojure-czech (1)
- # clojure-europe (26)
- # clojure-nl (1)
- # clojure-uk (6)
- # core-matrix (1)
- # cursive (8)
- # datomic (20)
- # docker (38)
- # emacs (2)
- # events (1)
- # fulcro (6)
- # funcool (6)
- # hyperfiddle (79)
- # introduce-yourself (1)
- # lsp (131)
- # malli (32)
- # off-topic (11)
- # pathom (3)
- # re-frame (11)
- # reagent (15)
- # releases (2)
- # shadow-cljs (49)
- # sql (3)
- # tools-deps (36)
Hi all,
Is it okay to have a component that wraps a r/with-let
in a let
or do I need to split them apart so that the bits with the r/with-let
are in their own component and has that at the top level?
Here's an example with them separate:
(defn inner []
(r/with-let [bar (r/atom {})]
;[some hiccup]
))
(defn outer []
(let [data-ready? (rf/subscribe [:foo])]
(when @data-ready?
[inner])))
And here's the equivalent if it were all inline:
(defn combined []
(let [data-ready? (rf/subscribe [:foo])]
(when @data-ready?
(r/with-let [bar (r/atom {})]
;[some hiccup]
))))
You can put everything in the same r/with-let
- you don't need let
at all in this case.
But using let
outside of r/with-let
should be fine.
Thanks for your input! Sorry, my example didn't clearly demonstrate but I was trying to suggest that some of the bindings I want to do in the with-let depend on the data being ready.
So from that I take it that I would be okay to go with something like the combined version above, and I wouldn't be accidentally breaking the intended behavior of the with-let if I do so.
Mm, when you have a data dependency then I'm not so sure. I'd test it, and perhaps there's already a test for it in the Reagent's source code.
(r/with-let [bar (r/atom {})]
(when @(rf/subscribe [:foo]) ...)
There is rarely reason to store subscribe value in let without the deref.What about that data dependency? As in
(defn view [x]
(let [a (rf/subscribe [:a])]
(r/with-let [b (r/reaction (+ @a x))]
...))
If you use re-frame, you would probably use another subscription that depends on the first.
(rf/sub :b
:<- [:a]
(fn [a [_ x]] (+ a x)))
@(rf/subscribe [:b x])
a
is only set once, but that is OK because it returns a reaction etc.
Or I think it works like that. Deciphering how the macro works is a pain. I often prefer form-2 components to with-let as it is easier to understand how that works.
(defn view [x]
(let [a (rf/subscribe [:a])]
b (r/reaction (+ @a x))]
(fn [_x]
...)))
I think with-let and form-2 version should work the same, if x
property changes, those changes aren't reflected on the reaction.