On Sunday, March 29, 2015 at 11:38:44 PM UTC+11, Colin Yates wrote:
> Hi all, I have read the docs but might have overlooked something.
> 
> I keep seeing components being updated in response to state changes but I 
> don't see the latest state in that component.
> 
> For example, in the following:
> 
> (defn- make-rows
>   [on-click selected-id]
>   (js/console.log "rows has selected-id: " selected-id)
>   [:tbody
>    (for [idx (range 10)]
>      [:tr
>       {:class    (when (= selected-id idx) "selected")
>        :on-click #(do (on-click idx) nil)}
>       [:td "Some state"]])])
> 
> (defn make-table []
>   (let [selected-id (subscribe [:search/selected-journey-id])]
>     (fn []
>       (js/console.log "make-table has selected-id: " @selected-id)
>       [table/table {:headers [{:class :ignore-me :text "whatever"}]
>                     :rows    (make-rows #(dispatch [:search/select-journey-id 
> %])
>                                         @selected-id)}])))
> 
> when I call [make-table] then I can see both make-table and make-rows being 
> called with the new selected-id, but table/table never gets the new 
> selected-id, it is always called with the old selected-id. It is as if the 
> invocation to (rows) is cached.
> 
> Changing make-table to use [make-rows] makes the problem worse as only 
> make-tables is called in response to the selected-id changing.
> 
> The only way I can get this to work is to make-rows itself subscribe to the 
> selected-id:
> 
> (defn- make-rows
>   [on-click]
>   (let [selected-id (subscribe [:search/selected-journey-id])]
>     (fn []
>       (js/console.log "rows has selected-id: " @selected-id)
>       [:tbody
>        (for [idx (range 10)]
>          [:tr
>           {:class    (when (= @selected-id idx) "selected")
>            :on-click #(do (on-click idx) nil)}
>           [:td "Some state"]])])))
> 
> (defn make-table []
>   [table/table {:headers [{:class :ignore-me :text "whatever"}]
>                 :rows    [make-rows #(dispatch [:search/select-journey-id 
> %])]}])
> 
> Can somebody please explain why calling an fn with a @subscription is the 
> wrong thing to do.
> 
> Thanks for reading this far ;).


Colin, any chance you can create a pared-back repo showing the issue?  My guess 
is that you can make it very simple by removing all use of re-frame and just 
using ratoms in place of subscriptions. 

So instead of something like:
    [blah   (subscribe [:something])]

you can simulate via: 
    [blah   (reagent/atom (some data))]      

Then you can reset! blah to simulate subscription changes.

After all, a subscription just returns something which behaves much like a 
ratom. 


--
Mike

-- 
Note that posts from new members are moderated - please be patient with your 
first post.
--- 
You received this message because you are subscribed to the Google Groups 
"ClojureScript" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/clojurescript.

Reply via email to