Clojurescript this-as 宏指向全局对象

标签 clojurescript reactjs

在 clojurescrtipt 中编写 ReactJS 教程时,我发现 this-as 宏编译为

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

它总是指向 react 类中的窗口。有时我可以通过 js* this 解决这个问题,但不能在 letmap 内部解决,因为它们也被编译为函数。

我如何在let表单中访问react js this

fiddle 情况:http://jsfiddle.net/VkebS/57/

还有一段教程仅供引用:

(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:我可以使用 native 数组作为数据和 native map 功能

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

这可行,但是......

最佳答案

this-as 如果您在 render 函数开始时使用它,则它会起作用:

(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))}}))))}))

另请参阅:https://github.com/swannodette/om/blob/master/src/om/dom.cljs#L34

关于Clojurescript this-as 宏指向全局对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20597568/

相关文章:

clojurescript - 是否等效于clojurescript中的$(document).ready(function(){})?

node.js - 服务器端渲染 (Next.js) 和静态站点渲染 (Gatsby.js) 有什么区别?

reactjs - 为什么即使我修改了草稿,Immer 也没有给我返回一个新状态?

javascript - 如何设置单选按钮默认选中 react ?

java - 如何将环境变量附加到 java clojure cli?

clojure - clojure 中主机互操作点宏是如何实现的

clojurescript - 从 javascript 回调返回值

javascript - React 虚拟化自动滚动问题

android - 如何使用 react-native 在 textInput 字段中自动获取 '/'(斜线)

javascript - cljs 未在 LightTable 中定义