2
votes

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)

2

2 Answers

1
votes

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.

4
votes

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