bluelemonade
bluelemonade

Reputation: 1305

clojurescript reagent swap! atom negate atom value

i tried to invert a value changeval of a reagent atom

(def changeval(reagent/atom 0))
...
...
[:input {:type "button" :value "Invert!"
            :on-click #(swap! changeval(not= changeval0 ) ) }]

nothing happens, how can I make the changeval = NOT(changeval)

Upvotes: 2

Views: 1510

Answers (2)

Piotrek Bzdyl
Piotrek Bzdyl

Reputation: 13185

Check the signature of swap! function:

(swap! atom f)

(swap! atom f x)

(swap! atom f x y)

(swap! atom f x y & args)

Atomically swaps the value of atom to be: (apply f current-value-of-atom args). Note that f may be called multiple times, and thus should be free of side effects. Returns the value that was swapped in.

Thus to negate the value in the atom (it works the same with Clojure's as well as Reagent's atoms) use not function (in your case there will be no additional args as you will use only the current value of the atom):

(def value (atom true))

(swap! value not)
;; => false

(swap! value not)
;; => true

Upvotes: 5

akond
akond

Reputation: 16060

The third parameter to swap! is a function. So I guess it should be:

#(swap! changeval (partial not= changeval0))

I think changeval0 is a typo and you want a boolean. In this case check out the Piotrek's answer.

Upvotes: 1

Related Questions