我想创建一个默认隐藏的 html 元素列表(包括查询结果),但用户可以切换该状态。我在下面尝试了几种不同的方法作为玩具示例,但都无法正常工作。
这段代码正确地创建了三个按钮,它们正确地改变了 exps 状态,但它们永远不会隐藏内容。
(:require [reagent.core :as r] )
(def exps (r/atom [true true true]))
(defn like-component []
[:div
(for [ [i r] (map-indexed vector ["A" "B" "C"])]
[:div
[:button {:on-click #(swap! exps update-in [i] not)}]
(when (nth @exps i)
[:pre (str i r)])])])
(r/render [like-component]
(js/document.getElementById "app"))
另一方面,下面的代码将只创建一个元素,但它可以正常工作。
(defn expandable-view [e bool]
(let [expanded (r/atom bool)]
(fn []
[:li
[:div.expandable
[:div.header {:on-click #(swap! expanded not)}
"Click me to expand and collapse"]
(if @expanded
[:div.body (allow-html :pre e)])]])))
(defn like-component []
[:ul
(vec
(for [ e ["A" "B" "C"]]
(expandable-view e true ))) ])
(r/render [like-component]
(js/document.getElementById "app"))
编辑:可能相关: https ://github.com/reagent-project/reagent/wiki/Beware-Event-Handlers-Returning-False