正确操作试剂状态
Correctly manipulating state in reagent
我刚刚学习 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)}]
]
)
我试图在按下按钮时增加状态,但我在控制台上收到此错误
Error: No protocol method ISwap.-swap! defined for type number: 0
atom 应该交换而不是 :click-count
(swap! app-state update :click-count inc)
我刚刚学习 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)}]
]
)
我试图在按下按钮时增加状态,但我在控制台上收到此错误
Error: No protocol method ISwap.-swap! defined for type number: 0
atom 应该交换而不是 :click-count
(swap! app-state update :click-count inc)