Lucky
Lucky

Reputation: 2082

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

Upvotes: 5

Views: 280

Answers (1)

Minh Tuan Nguyen
Minh Tuan Nguyen

Reputation: 1054

the atom should be swapped not the :click-count

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

Upvotes: 6

Related Questions