Fork me on GitHub
#reagent
<
2017-12-26
>
zalky15:12:29

I've been using reagent.ratom/run! to replace react life-cycle methods with simple FRP-ish computations. So for example:

(defn component
  []
  (r/with-let [...
               _ (r/run! (do-something @signals))]
    [:div ...]))
Where (do-something @signals) would otherwise need to be implemented across a number of life-cycle methods. I need these reactions to run right away, which is why I used run! instead of reaction. However, I've noticed that, reactions created with run! persist after a component has been unmounted, unliked those created by reaction. This was unexpected. Looking at the source, it seems that any reaction with :auto-run might never be disposed: https://github.com/reagent-project/reagent/blob/master/src/reagent/ratom.cljs#L355 I'm curious why that is? I supposed I can simply use @(reaction ...) to get a disposable reaction that is run right away, but then what is the use case of run!? Where might you use a reaction that will never be disposed?

zalky22:12:21

I realize now that @(reaction ...) is insufficient, you need something more like:

(defn component
  []
  (r/with-let [...
               r (r/reaction (do-something @signals))]
    @r
    [:div ...]))
If you don't dereference r with each render, the reaction will not be re-computed (hence the purpose of :auto-run). Would be much more convenient if reactions with :auto-run (i.e. run!) could be disposed.