Question

During writting reactjs tutorial in clojurescrtipt i've found that this-as macro compiles to

(function(){var t = this; return t;}

which always points to window inside react classes. Sometimes i can workaround this by js* this but not inside let or map cuz they are also compiled to functions.

How can i access react js this inside let form?

Situation on fiddle: http://jsfiddle.net/VkebS/57/

and a piece of tutorial FYI:

(def comment-list 
  (React/createClass 
   #js{:render 
       (fn [] (dom/div #js {:className "commentList"}
                       (let [d (this-as t (.. t -props -data))]
                         (map #(commnt #js {:author (:author %)} (:text %)) d))))}))

PS: i can use native array for data and native map function

(def comment-list 
  (React/createClass 
   #js{:render 
       (fn [] (dom/div #js {:className "commentList"}
                       (.map (.. (js* "this") -props -data) #(commnt #js {:author (:author %)} (:text %)))))}))

that works, but...

Was it helpful?

Solution

this-as works if you use it at start of render function:

(def commnt
  (React/createClass
   #js {:render
        (fn []
          (this-as this
                   (dom/div #js {:className "comment"}
                            (dom/h2 #js {:className "commentAuthor"}
                                    (.. this -props -author))
                            (dom/span #js {:dangerouslySetInnerHTML
                                           #js{:__html
                                               (.makeHtml converter (..  (js* "this") -props -children toString))}}))))}))

see also: https://github.com/swannodette/om/blob/master/src/om/dom.cljs#L34

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top