【问题标题】:clojure: reagent: how to create reaction atomclojure: 试剂: 如何创建反应原子
【发布时间】:2017-03-29 16:40:46
【问题描述】:

Github/谷歌

我知道答案在 Google/Github 上。 (我知道这是可能的,因为我在过去的某个时间阅读了它的文档。)但是,我似乎根本找不到它。

一般背景

Clojure 试剂有一个“r/atom”形式

  (def some-input (r/atom ""))

然后,如果我定义一个组件,比如:

(fn []
  [:div @some-input])

它具有当一些输入原子改变时,div 更新的属性。

反应原子

Clojure 试剂有一种叫做“反应”的东西,我们可以在其中定义:

(def ra (r/atom ""))
(def rb ... I HAVE NO IDEA WHAT TO PUT HERE ...)

具有 @rb = (f @ra) 的属性——这样每当 ra 更新时,rb 就会自动更新为 (f @ra)

我相信这被称为“反应原子”之类的——但我找不到它。

实际问题:

我在里面放什么:

(def rb ... I HAVE NO IDEA WHAT TO PUT HERE ...)

编辑已解决:

记录在案https://github.com/Day8/re-frame (另一个非常酷的项目,也使用了 Reagent)

具体例子是:

(def app-db  (reagent/atom {:a 1}))           ;; our root ratom  (signal)

(def ratom2  (reaction {:b (:a @app-db)}))    ;; reaction wraps a computation, returns a signal
(def ratom3  (reaction (condp = (:b @ratom2)  ;; reaction wraps another computation
                         0 "World"
                         1 "Hello")))

【问题讨论】:

  • 我对试剂一无所知,但我认为这是来自github 的相关文档:Any component that dereferences a reagent.core/atom will be automatically re-rendered. - 所以它会自动发生吗?编辑:或者track
  • @birdspider:很好的尝试!它实际上是“反应”github.com/Day8/re-frame(问题是:它似乎不在试剂 github 上,而是在 使用试剂的库的文档上)
  • 啊它是ratomns 中的一个宏@

标签: clojure reagent


【解决方案1】:

复制问题的答案只是为了将问题标记为已解决:

(def app-db  (reagent/atom {:a 1}))           ;; our root ratom  (signal)

(def ratom2  (reaction {:b (:a @app-db)}))    ;; reaction wraps a computation, returns a signal
(def ratom3  (reaction (condp = (:b @ratom2)  ;; reaction wraps another computation
                     0 "World"
                     1 "Hello")))

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-21
  • 1970-01-01
  • 2015-06-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多