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])Right. But will the above work? :)
It should
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.