gpt4 book ai didi

clojurescript - 试剂组件未更新

转载 作者:行者123 更新时间:2023-12-04 15:30:01 25 4
gpt4 key购买 nike

' 添加 ' 和 ' 删除 ' 购物 list 中的商品更新了 'shopping-list' 组件,但 ' 更新 ' 没有。

我使用 cljs REPL 来更新购物 list 。

添加 :

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=>

购物 list 已更新,我在 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:我没有将本地状态放入我的示例中,但我的真实应用程序包含本地状态器。据此,我必须为嵌入的渲染函数设置与组件函数相同的参数。我修改了我的例子,添加了本地状态随机数和参数渲染函数,它运行良好。
(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)))

最佳答案

删除 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 组件的此文档:

https://github.com/reagent-project/reagent/blob/master/doc/CreatingReagentComponents.md#form-2--a-function-returning-a-function

关于clojurescript - 试剂组件未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48964742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com