[英]How to filter a list based on user input with ClojureScript and Om?
[英]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.