Reputation: 313
I'm building a single-page app using re-frame. Each "page" of the app calls a component base-page
then supplies its page-specific children...
(defn base-page [& children]
(into [:div
; banner goes here
] children))
(register-sub :count
(fn [db _] (reaction (:count @db))))
(register-handler :inc
(fn [db _] (update db :count inc)))
(defn test-page []
(let [count (subscribe [:count])]
(fn []
[base-page
[:h2 "Test Page"]
[:p (str "Count: " @count)]])))
This renders the page correctly with the initial value for :count
, and when I run (dispatch [:inc])
the page updates correctly.
Now I'd like base-page
to be a form-2 component so it can have it's own subscriptions...
(defn base-page [& children]
(let [user (subscribe [:current-user])]
(fn []
(into [:div
; banner goes here, including @user
] children))
However, when I do this, the count on test-page
no longer updates when I run (dispatch [:inc])
. I find this surprising, because I thought Reagent packages form-1 and form-2 components into form-3 components behind the scenes.
Is this expected behaviour? If so, is there a better way to implement my base page/concrete page model?
Upvotes: 4
Views: 1294
Reputation: 6509
Isn't it a rule in the re-frame Reagent documentation (*) that for Form-2 components the outer function and the inner function must have the same arguments?
Check out the second 'rookie mistake' on this page
(*) re-frame depends on Reagent. The documentation about Form-1/2/3 components applies equally to using Reagent on its own or from one of the other frameworks/libraries that sit on top of Reagent.
Upvotes: 4
Reputation: 313
As Chris Murphy pointed out, the problem was that I wasn't passing the outer arguments to my inner function. This definition of base-page
works fine:
(defn base-page [& children]
(let [user (subscribe [:current-user])]
(fn [& children]
(into [:div
; banner goes here, including @user
] children))
Upvotes: 3