简体   繁体   English

clojurescript试剂更换! 原子取反原子值

[英]clojurescript reagent swap! atom negate atom value

i tried to invert a value changeval of a reagent atom 我试图反转试剂原子的值changeval

(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) 什么都没发生,我怎样才能使changeval = NOT(changeval)

Check the signature of swap! 检查swap!的签名swap! function: 功能:

(swap! atom f)

(swap! atom fx)

(swap! atom fxy)

(swap! atom fxy & args)

Atomically swaps the value of atom to be: (apply f current-value-of-atom args) . 以原子方式将原子的值交换为:( (apply f current-value-of-atom args) Note that f may be called multiple times, and thus should be free of side effects. 注意,f可以被多次调用,因此应该没有副作用。 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): 因此,要抵消原子中的值(它与Clojure的原子和试剂的原子相同) not使用功能(在您的情况下,将不存在其他参数,因为您将仅使用原子的当前值):

(def value (atom true))

(swap! value not)
;; => false

(swap! value not)
;; => true

The third parameter to swap! 第三个参数要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. 我认为changeval0是一个拼写错误,您想要一个布尔值。 In this case check out the Piotrek's answer. 在这种情况下,请查看Piotrek的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM