Fork me on GitHub
#reagent
<
2021-02-09
>
lsenjov03:02:37

I’m having what I believe is a major misunderstanding of reactions. As far as I know, reactions should only be updating when the reactions/atoms they rely on update, but this doesn’t appear to be the case

(def a0 (r/atom {:a 0}))
  (def a1 (ratom/reaction (println "proc: a1") (update @a0 :a inc)))
  (def a2 (ratom/reaction (println "proc: a2") (update @a1 :a inc)))
  (def a3 (ratom/reaction (println "proc: a3") (update @a2 :a inc)))
=> #'user/a0
=> #'user/a1
=> #'user/a2
=> #'user/a3
@a3
proc: a3
proc: a2
proc: a1
=> {:a 3}
@a3
proc: a3
proc: a2
proc: a1
=> {:a 3}

lsenjov03:02:52

I expect the first deref to come up with the printlns, but I don’t expect it to the second time

lsenjov03:02:03

Since the input atoms/reactions haven’t changed in the meantime

lsenjov03:02:01

This is reagent 1.0.0

lsenjov03:02:39

Is there a way to ensure things only update when the underlying atoms update?

p-himik06:02:08

This part of Reagent is not really documented, but reactions are reactive only when *ratom-context* is set. I.e. when rendering views.

👍 3
lsenjov22:02:03

Ah I see. I ended up using track! to make it reactive. Any issues with that off the top of your head?

p-himik06:02:43

Sorry, no idea - I've never used it.

👍 3