试剂组件无法更新

时间:2018-02-24 16:04:02

标签: clojurescript reagent

' 添加'和' 删除' shoppinglist ratom的商品更新了购物清单'组件,但' 更新'没有按'吨。

我使用cljs REPL来更新shoppinglist ratom。

添加

shopping.app=> (swap! shoppinglist assoc 3 {:id 3, :name "Coke", :price 25})
WARNING: Use of undeclared Var shopping.app/shoppinglist at line 1 <cljs repl>
{1 {:id 1, :name "Bread", :price 23}, 2 {:id 2, :name "Milk", :price 12}, 3 {:id 3, :name "Coke", :price 25}}

删除

shopping.app=> (swap! shoppinglist dissoc 3)
WARNING: Use of undeclared Var shopping.app/shoppinglist at line 1 <cljs repl>
{1 {:id 1, :name "Bread", :price 20}, 2 {:id 2, :name "Milk", :price 12}}

更新

shopping.app=> (swap! shoppinglist assoc 2 {:id 2, :name "Milk", :price 8})
WARNING: Use of undeclared Var shopping.app/shoppinglist at line 1 <cljs repl>
{1 {:id 1, :name "Bread", :price 20}, 2 {:id 2, :name "Milk", **:price 8**}}
shopping.app=>

购物清单鼠标已更新,我在REPL中检查了它,但组件未更新。

(ns shopping.app
  (:require [reagent.core :as r]))

(defonce shoppinglist (r/atom (sorted-map
                           1 {:id 1 :name "Bread" :price 20},
                           2 {:id 2 :name "Milk" :price 12})))

(defn shopping-item [item]
  (let [{:keys [id name price]} item]
    (fn []
      [:div
        [:label id]
        [:label (str " | " name)]
        [:label (str " | " price)]])))

(defn shopping-list []
  [:div.container
    (for [item (vals @shoppinglist)]
      ^{:key (:id item)} [:div
        [shopping-item item]])])

(defn init
  "Initialize components."
  []
  (let [container (.getElementById js/document "container")]
    (r/render-component 
      [shopping-list] 
      container)))

已编辑==================================

我在这里找到了关于组件设计的一个很好的概述https://github.com/reagent-project/reagent/blob/master/docs/CreatingReagentComponents.md

表单2:我没有将本地状态放入我的示例中,但我的真实应用程序包含本地stater ratom。据此,我必须为嵌入式渲染函数添加与组件​​函数相同的参数。我修改了我的例子,添加了本地状态ratom和params渲染函数,它运行良好。

(ns shopping.app
  (:require [reagent.core :as r]))

(defonce shoppinglist (r/atom (sorted-map
                           1 {:id 1 :name "Bread" :price 20},
                           2 {:id 2 :name "Milk" :price 12})))

(defn shopping-item [{:keys [id name price]}]
  (let [loacalstate (r/atom true)]
    (fn [{:keys [id name price]}]
      [:div
        [:label id]
        [:label (str " | " name)]
        [:label (str " | " price)]])))

(defn shopping-list []
  [:div.container
    (for [item (vals @shoppinglist)]
      ^{:key (:id item)} [:div
        [shopping-item item]])])

(defn init
  "Initialize components."
  []
  (let [container (.getElementById js/document "container")]
    (r/render-component 
      [shopping-list] 
      container)))

1 个答案:

答案 0 :(得分:2)

删除shopping-item中的内部无参数包裹函数。这是不必要的,但会阻止重新呈现现有项目,因为no-arg函数没有看到更改的参数。

所以:

(defn shopping-item [item]
  (let [{:keys [id name price]} item]
    [:div
     [:label id]
     [:label (str " | " name)]
     [:label (str " | " price)]]))

(defn shopping-item [{:keys [id name price]}]
  [:div
   [:label id]
   [:label (str " | " name)]
   [:label (str " | " price)]])

有关更多信息,请查看form-2组件的此文档:

this