Fork me on GitHub
#reagent
<
2023-03-16
>
Ziad Salah17:03:25

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]
        ))))

p-himik17:03:13

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.

Ziad Salah17:03:45

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.

Ziad Salah17:03:19

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.

p-himik17:03:37

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.

👍 2
juhoteperi17:03:45

(r/with-let [bar (r/atom {})]
  (when @(rf/subscribe [:foo]) ...)
There is rarely reason to store subscribe value in let without the deref.

p-himik17:03:06

What about that data dependency? As in

(defn view [x]
  (let [a (rf/subscribe [:a])]
    (r/with-let [b (r/reaction (+ @a x))]
      ...))

juhoteperi17:03:49

If you use re-frame, you would probably use another subscription that depends on the first.

juhoteperi17:03:51

(rf/sub :b
  :<- [:a]
  (fn [a [_ x]] (+ a x)))

@(rf/subscribe [:b x])

p-himik17:03:25

Right. But will the above work? :)

juhoteperi17:03:35

It should

👍 2
juhoteperi17:03:10

a is only set once, but that is OK because it returns a reaction etc.

juhoteperi17:03:30

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.

juhoteperi17:03:33

(defn view [x]
  (let [a (rf/subscribe [:a])]
        b (r/reaction (+ @a x))]
    (fn [_x]
      ...)))

juhoteperi17:03:28

I think with-let and form-2 version should work the same, if x property changes, those changes aren't reflected on the reaction.