Nyo*_*kti 2 clojure clojurescript reagent re-frame
我最近使用试剂和重新框架为我的clojurescript项目,我有一个问题:所以我有html自定义标签
<question id="1"></question>
<question id="2"></question>
Run Code Online (Sandbox Code Playgroud)
我想用cljs函数将它们交换到我的试剂生成的html中
(defn mypanel []
[:p "Hi!"])
(let [q (.getElementsByTagName js/document "question")]
(for [i (range 2)]
^{:keys i}
(reagent/render [mypanel]
(aget (.getElementsByTagName js/document "question") i))))
Run Code Online (Sandbox Code Playgroud)
但它不起作用,我试图测试它而不使用for函数
(reagent/render [mypanel]
(aget (.getElementsByTagName js/document "question") 0))
Run Code Online (Sandbox Code Playgroud)
只用一个标签就可以了.
而且我不知道为什么for功能不起作用,或者试剂不能这样工作?有人有建议吗?
我非常喜欢这个.
for产生一个惰性序列,这意味着在需要时不会完成评估序列的工作.你不能使用延迟序列来强制副作用,因为它们永远不会被评估(render就是这样一个地方).为了强制副作用你应该用它替换它doseq.在你的情况下dotimes可能会更好:
(let [q (.getElementsByTagName js/document "question")]
(dotimes [i 2]
^{:keys i}
(reagent/render [mypanel]
(aget (.getElementsByTagName js/document "question") i))))
Run Code Online (Sandbox Code Playgroud)