5

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

Ampersanda
  • 2,016
  • 3
  • 21
  • 36

1 Answers1

6

the atom should be swapped not the :click-count

(swap! app-state update :click-count  inc)
Minh Tuan Nguyen
  • 1,026
  • 8
  • 13
  • Wow.. I was trying (swap! @app-state update-in [:click-count] inc), but doesn't work. Could you tell me, why the @ symbol is not required? – Ampersanda Sep 20 '17 at 14:12
  • 2
    With @ you only get the current state of the atom, to change the state you must swap the atom itself. – Minh Tuan Nguyen Sep 20 '17 at 14:13
  • @MochamadLuckyPradana Your question is more Clojure-specific than Reagent-specific. Try it out in a repl, before doing it in a web-context. It's what makes Clojure(Script) fun :-) – Grav Oct 06 '17 at 21:43
  • @Grav : Thank you for the advice. I have a problem with some English language so maybe it will help myself by doing it on the way.. sorry ._. – Ampersanda Oct 07 '17 at 07:33