繁体   English   中英

在clojurescript中,如何评估列表

[英]In clojurescript, how to evaluate a list

假设有

(def defining-list `(def one 1))

我如何评估定义列表,使之成为1? (在clojurescript中)

编辑:我将给出更广泛的图像的概念,以及我在这里想要避免发生的X / y问题。

我正在尝试使用cljsjs包中的cljsjs / material-ui,而不是每次定义一个react组件来使用它,如下所示:

(def app-bar 
  (r/adapt-react-class (aget js/MaterialUI (name :AppBar)))

我想从标签数组定义所有组件:

(def material-ui-tags '[AppBar Avatar Backdrop])

所以我在想如果不使用宏就可以做到这一点,因为我发现了这一点

就像是:

(doseq [component material-ui-tags]
  `(def ~(symbol (->kebab-case component)) (r/adapt-react-class (aget js/MaterialUI ~(name component)))))

但是上面只创建了一个def列表,我想对它们进行评估。 在clojure中,eval可以解决问题。

使用试剂,您可以使用:>作为adapt-react-class简写,如https://github.com/reagent-project/reagent/blob/master/docs/InteropWithReact.md中所述

另外,您可以在js/使用点符号,并且我认为在1.9.854以上的shadow-cljs或cljs中,您可能require导入符号而不是使用aget

在您的情况下,它将类似于:

(ns example.core
  (:require [MaterialUI]))

(defn component-two []
  [:> MaterialUI/AppBar {:some-prop "some-value"}
    [:div "children-here"]])

(defn component-two []
  ;; If the require above doesn't work
  [:> js/MaterialUI.AppBar {:some-prop "some-value"}
    [:div "children-here"]])

要使用def进行所需的操作,您需要eval或macro。 正如贾里德·史密斯(Jared Smith)在评论中解释的那样,评估并不理想。

您从agent-material-ui链接的示例使用了宏。 调用宏实际上会执行扩展,然后进行评估。 因此,您的代码必须是这样的:

clj文件

(def material-ui-tags '[AppBar Avatar Backdrop])

(defmacro adapt-components []
  (for [component material-ui-tags]
    `(def ~(symbol (->kebab-case component)) (reagent.core/adapt-react-class (aget js/MaterialUI ~(name component))))))

cljs文件

(adapt-components) ;; your defs will be available below this line

(defn my-component []
  [app-bar ...])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM