rum.core
(cursor-in ref path & {:as options})
Given atom with deep nested value and path inside it, creates an atom-like structure
that can be used separately from main atom, but will sync changes both ways:
(def db (atom { :users { "Ivan" { :age 30 }}}))
(def ivan (rum/cursor db [:users "Ivan"]))
\@ivan ;; => { :age 30 }
(swap! ivan update :age inc) ;; => { :age 31 }
\@db ;; => { :users { "Ivan" { :age 31 }}}
(swap! db update-in [:users "Ivan" :age] inc) ;; => { :users { "Ivan" { :age 32 }}}
\@ivan ;; => { :age 32 }
Returned value supports deref, swap!, reset!, watches and metadata.
The only supported option is `:meta`
(defc & body)
Defc does couple of things:
1. Wraps body into sablono/compile-html
2. Generates render function from that
3. Takes render function and mixins, builds React class from them
4. Using that class, generates constructor fn [args]->ReactElement
5. Defines top-level var with provided name and assigns ctor to it
(rum/defc label [t]
[:div t])
;; creates React class
;; defines ctor fn (defn label [t] ...) => element
(label "text") ;; => returns React element built with label class
Usage:
(defc name doc-string? [< mixins+]? [params*] render-body+)
(defcc & body)
Same as defc, but render will take additional first argument: react component
Usage:
(defcc name doc-string? [< mixins+]? [comp params*] render-body+)
(defcs & body)
Same as defc, but render will take additional first argument: state
Usage:
(defcs name doc-string? [< mixins+]? [state params*] render-body+)
derived-atom
Use this to create “chains” and acyclic graphs of dependent atoms.
`derived-atom` will:
- Take N “source” refs
- Set up a watch on each of them
- Create “sink” atom
- When any of source refs changes:
- re-run function `f`, passing N dereferenced values of source refs
- `reset!` result of `f` to the sink atom
- return sink atom
(def *a (atom 0))
(def *b (atom 1))
(def *x (derived-atom [*a *b] ::key
(fn [a b]
(str a ":" b))))
(type *x) ;; => clojure.lang.Atom
\@*x ;; => 0:1
(swap! *a inc)
\@*x ;; => 1:1
(reset! *b 7)
\@*x ;; => 1:7
Arguments:
refs - sequence of source refs
key - unique key to register watcher, see `clojure.core/add-watch`
f - function that must accept N arguments (same as number of source refs)
and return a value to be written to the sink ref.
Note: `f` will be called with already dereferenced values
opts - optional. Map of:
:ref - Use this as sink ref. By default creates new atom
:check-equals? - Do an equality check on each update: `(= @sink (f new-vals))`.
If result of `f` is equal to the old one, do not call `reset!`.
Defaults to `true`. Set to false if calling `=` would be expensive
render-html
Main server-side rendering method. Given component, returns HTML string with
static markup of that component. Serve that string to the browser and
`rum.core/mount` same Rum component over it. React will be able to reuse already
existing DOM and will initialize much faster
render-static-markup
Same as `rum.core/render-html` but returned string has nothing React-specific.
This allows Rum to be used as traditional server-side template engine