Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested components in re-frame not updating

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?

like image 801
John Krasnay Avatar asked Mar 14 '23 17:03

John Krasnay


1 Answers

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.

like image 137
Chris Murphy Avatar answered Apr 29 '23 09:04

Chris Murphy