在Reagent中正确操作状态

5
我刚学习Clojurescript中的Reagent,我正在跟随一些教程,但可能我错过了什么。下面是我用于状态的代码。
(defonce app-state (atom {:text "Hello Chestnut!" :click-count 0}))

"和渲染后的视图"
(defn article []
  [:div
   [:div "The atom" [:code "click-count"] " has value : " (:click-count @app-state)]
   [:input {:type "button" :value "Add"
            :on-click #(swap! (:click-count @app-state) inc)}]
   ]
  )

我试图在按下按钮时递增状态,但是我在控制台上得到了这个错误。
错误信息为:没有为类型数字(0)定义ISwap.-swap!协议方法。

1
有用的文档:https://clojuredocs.org/clojure.core/swap! - BillRobertson42
1
我已经阅读了,但还是谢谢 :) - Ampersanda
1个回答

6

the atom should be swapped not the :click-count

(swap! app-state update :click-count  inc)

哇..我尝试了(swap! @app-state update-in [:click-count] inc),但不起作用。你能告诉我,为什么不需要@符号吗? - Ampersanda
2
使用@符号只能获取原子的当前状态,要改变状态必须交换原子本身。 - Minh Tuan Nguyen
@MochamadLuckyPradana 你的问题更多关于Clojure,而不是Reagent。在Web环境中操作之前,请在repl中尝试它。这是使Clojure(Script)有趣的原因 :-) - Grav
@Grav:谢谢你的建议。我有些英语问题,也许这样做可以帮助我自己。抱歉。._. - Ampersanda

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接