列表项试剂 clojurescript 的单击处理程序
on-click handler for a list item reagent clojurescript
我想为列表中的每个项目添加一个点击处理程序。
(defonce selected-department (atom "department!"))
(defn sidebar []
[:div#sidebar-wrapper
[:ul.sidebar-nav
[:li.sidebar-brand [:a {:href "#"} "Departments"]]
;;[:li [:a {:on-click (reset! selected-department "test!")} "Dairy"]]
[:li [:a {:href "#"} "Dairy"]]
[:li [:a {:href "#"} "Deli"]]
[:li [:a {:href "#"} "Grocery"]]]])
然后selected-department是我要show/use数据的标签
(defn response-box []
[:div#form_comparison
[:label#dlabel @selected-department]])
注释掉的代码段不起作用。有没有办法使这项工作?
您的示例无法正常工作,因为您必须像这样将函数传递给 :on-click :
[:li [:a {:on-click #(reset! selected-department "test!")} "Dairy"]]
所以您唯一需要更改的是在 (reset! ...
相当于
[:li [:a {:on-click (fn [_] (reset! selected-department "test!"))} "Dairy"]]
编辑:
这是我测试过的完整代码,对我来说工作正常:
(defonce selected-department (atom "department!"))
(defn sidebar []
[:div#sidebar-wrapper
[:ul.sidebar-nav
[:li.sidebar-brand [:a {:href "#"} "Departments"]]
[:li [:a {:on-click #(reset! selected-department "Dairy") :href "#"} "Dairy"]]
[:li [:a {:on-click #(reset! selected-department "Deli") :href "#"} "Deli"]]
[:li [:a {:on-click #(reset! selected-department "Grocery") :href "#"} "Grocery"]]]
[:label @selected-department]])
(reagent/render-component [sidebar] (.getElementById js/document "app"))
单击列表中的项目时,底部的标签会更新。
我想为列表中的每个项目添加一个点击处理程序。
(defonce selected-department (atom "department!"))
(defn sidebar []
[:div#sidebar-wrapper
[:ul.sidebar-nav
[:li.sidebar-brand [:a {:href "#"} "Departments"]]
;;[:li [:a {:on-click (reset! selected-department "test!")} "Dairy"]]
[:li [:a {:href "#"} "Dairy"]]
[:li [:a {:href "#"} "Deli"]]
[:li [:a {:href "#"} "Grocery"]]]])
然后selected-department是我要show/use数据的标签
(defn response-box []
[:div#form_comparison
[:label#dlabel @selected-department]])
注释掉的代码段不起作用。有没有办法使这项工作?
您的示例无法正常工作,因为您必须像这样将函数传递给 :on-click :
[:li [:a {:on-click #(reset! selected-department "test!")} "Dairy"]]
所以您唯一需要更改的是在 (reset! ...
相当于
[:li [:a {:on-click (fn [_] (reset! selected-department "test!"))} "Dairy"]]
编辑:
这是我测试过的完整代码,对我来说工作正常:
(defonce selected-department (atom "department!"))
(defn sidebar []
[:div#sidebar-wrapper
[:ul.sidebar-nav
[:li.sidebar-brand [:a {:href "#"} "Departments"]]
[:li [:a {:on-click #(reset! selected-department "Dairy") :href "#"} "Dairy"]]
[:li [:a {:on-click #(reset! selected-department "Deli") :href "#"} "Deli"]]
[:li [:a {:on-click #(reset! selected-department "Grocery") :href "#"} "Grocery"]]]
[:label @selected-department]])
(reagent/render-component [sidebar] (.getElementById js/document "app"))
单击列表中的项目时,底部的标签会更新。