Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correctly manipulating state in reagent

I just learn Reagent in Clojurescript, I am just following some tutorial but maybe I miss something I have this code for the state

(defonce app-state (atom {:text "Hello Chestnut!" :click-count 0}))

and the rendered view

(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)}]
   ]
  )

I'm trying to increment the state when they button is pressed, but I got this error on the console

Error: No protocol method ISwap.-swap! defined for type number: 0

like image 581
Ampersanda Avatar asked Sep 20 '17 14:09

Ampersanda


Video Answer


1 Answers

the atom should be swapped not the :click-count

(swap! app-state update :click-count  inc)
like image 146
Minh Tuan Nguyen Avatar answered Sep 30 '22 10:09

Minh Tuan Nguyen