reagent

Ziad Salah 2023-03-16T17:19:25.556169Z

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-himik 2023-03-16T17:25:13.818799Z

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 Salah 2023-03-16T17:28:45.436929Z

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 Salah 2023-03-16T17:29:19.092849Z

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-himik 2023-03-16T17:34:37.673199Z

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.

👍 1
juhoteperi 2023-03-16T17:45:45.934809Z

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

p-himik 2023-03-16T17:47:06.229689Z

What about that data dependency? As in

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

juhoteperi 2023-03-16T17:47:49.450569Z

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

juhoteperi 2023-03-16T17:48:51.344259Z

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

@(rf/subscribe [:b x])

p-himik 2023-03-16T17:49:25.487999Z

Right. But will the above work? :)

juhoteperi 2023-03-16T17:49:35.315569Z

It should

👍 1
juhoteperi 2023-03-16T17:50:10.956989Z

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

juhoteperi 2023-03-16T17:51:30.652159Z

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.

juhoteperi 2023-03-16T17:53:33.129479Z

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

juhoteperi 2023-03-16T17:54:28.691749Z

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