eav db
eav db

Reputation: 595

clojure: reagent: how to create reaction atom

Github/Google

I know that the answer is out there on Google/Github. (I know this is possible because I read documentation for it at some past point.) However, I can't seem to find it at all.

General Background

Clojure Reagent has an "r/atom" of the form

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

Then, if I define a component, like:

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

It has the property that when the some-input atom changes, the div updates.

Reaction Atom

Clojure Reagent has something called "reaction", where we can define:

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

with the property that @rb = (f @ra) -- in such a way so that whenever ra is updated, rb automatically updates to (f @ra)

I believe this is called "reaction atom" or something -- but I can't find it.

Actual question:

What do I put in the:

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

Edit Resolved:

It's documented https://github.com/Day8/re-frame (another very cool project, which also uses Reagent)

The concrete example is:

(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")))

Upvotes: 3

Views: 1889

Answers (1)

eav db
eav db

Reputation: 595

Duplicating answer from Question just to mark question as resolved:

(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")))

Upvotes: 4

Related Questions