On Sunday, March 8, 2015 at 5:51:06 PM UTC+11, Nils Blum-Oeste wrote:
> Thanks Mike, I had seen that already. IMHO it does not really explain this 
> properly because props are only used in the render function in that example 
> but not in the lifecycle hooks.
> 
> My current solution is to use `reagent.core/props` (public api, not an impl. 
> like get-props, just a thin wrapper for it though) in the hooks. Here is a 
> small working example. Pay attention to the usage of `props` in the first let 
> block.
> 
> Is this supposed to be the idiomatic way to do it? 
> 
> (defn- plot
>    ([comp]
>     (let [data [{:label "foo"
>                      :data (-> comp reagent/props :data)}]
>       (.plot js/$ (reagent/dom-node comp) (clj->js data) (clj->js 
> plot-options)))))
> 
>    (defn plot-component []
>        (reagent/create-class
>          {:component-did-mount plot
>           :component-did-update plot
>           :display-name "plot-component"
>           :reagent-render (fn []
>                             [:div.plot-container {:style {:width "100%"
>                                                           :height 
> "500px"}}])}))
> 
> ;; use the component like this:
> ; [plot/plot-component {:data (:plot-data @app-state)]]]])
> 
> 
> As you can see, I do not have explicit props in the component function 
> parameters even though I pass it when using the component. Is that bad 
> practice?


Okay, I get it now.  You want your lifecycle functions to get access to the 
latest props.  The goal is give the js library (which really owns the div) the 
data it needs to redraw the component. 

I've never had to do this, so I'm not sure what idiomatic would be, but:
  - there's nothing wrong using that public API   "props" etc
  - it looks like your approach is used within the React world, so that's a 
good sign: 
http://nicolashery.com/integrating-d3js-visualizations-in-a-react-app/

Just in case, you could create a ticket on Github and see what feedback you get 
there:  https://github.com/reagent-project/reagent/issues/


> 
> I was struggling to get this whole thing working because I had another, only 
> slightly related bug: I was passing props to the component not as a map (as 
> can be seen above), but used a vector directly (the only prop I needed). This 
> caused weird behaviours.

Remember that any vector is going to be interpreted as a component. Even when 
you don't want it to be. :-)

[X Y Z]  is always going to be seen as a component X with props Y and Z, even 
if you just want that vector to be regarded as pure data for the surrounding 
component. Such is Hiccup.

--
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