Fork me on GitHub
#re-frame
<
2022-10-05
>
valerauko14:10:41

when a component has subscriptions, is it preferable to use form-2?

(defn view
  []
  (let [a @(subscribe [::a])
        b @(subscribe [::b])]
    [:ul
     [:li (str a)]
     [:li (str b)]]))
vs
(defn view
  []
  (let [a (subscribe [::a])
        b (subscribe [::b])]
    (fn []
      [:ul
       [:li (str @a)]
       [:li (str @b)]])))

p-himik14:10:15

Doesn't matter in practice because re-frame subscriptions are cached. I always use the top variant unless the component is already a form-2 one for some unrelated reason.

valerauko14:10:04

thanks! good to know. i prefer the top variant too

Ferdinand Beyer15:10:00

This is especially important if your query vector depends on parameters:

(defn view [param]
  (let [a (subscribe [::a param])]
    (fn []
      [:div (str @a)])))
Here, the subscription will not update when the view is re-rendered with a different param! @(subscribe ...) guards you from this.

2