Fork me on GitHub
Petrus Theron08:09:18

OK, so it looks like my subscription woes may be related to reaction. This works:

  (fn [db [_ tbl id]]
     (get-in @db [:entities tbl id])))
But this doesn't:
  (fn [db [_ tbl id]]
     (reaction (get-in @db [:entities tbl id]))))

Petrus Theron12:09:34

I'm having a hard time passing reagent component arguments as arguments to subscriptions. Even if the function arguments call changes at the caller level, the subscription does not seem to reflect the changed argument. For example:

(defn some-entity-view [id]
  (let [entity (subscribe [:entities :users id] ; I suspect id is being captured here?
    (fn [id]
        [:div "id:" id ; this id updates when fn args change
            "entity:" (str @entity) ; but when id changes, entity does not.
Where the subscription looks like this:
  (fn [db [_ tbl id]
    (reaction (get-in @db [:entities tbl id]))))
It seems like the subscription captures the initial arguments. If that is the case, it significantly reduces the usefulness of parameterised subscriptions.


Can you use reaction inside a subscription? What is the reason for using reaction inside a subscription?

Petrus Theron13:09:36

@roberto, yes. The documentation suggests them to prevent unnecessary re-renders when parent ratoms change:


@petrus - looks like you are only subscribing one time (the let) which returns a reaction on that certain value in the data-store. Changing the id doesn't rerun the subscription because id isn't itself a reaction. Entity is watching the value of that item in the data-store for changes. I think there are 2 options: 1) make id a reaction or ratom and deref it in the subscription in the let, or 2) move the subscription inside the inner fn, so you get a new entity reaction for the new id. AFK, so I can't try it out right now, but one of those should work.