Add state-atom (again)

This commit is contained in:
Dan Holmsand 2015-02-07 09:39:33 +01:00
parent 7641288982
commit b574d8cc5a
2 changed files with 16 additions and 20 deletions

View File

@ -114,27 +114,34 @@ Everything is optional, except either :reagent-render or :render.
[]
comp/*current-component*)
(defn state
"Returns the state of a component, as set with replace-state or set-state."
(defn state-atom
"Returns an atom containing a components state."
[this]
(assert (util/reagent-component? this))
;; TODO: Warn if top-level component
(comp/state this))
(comp/state-atom this))
(defn state
"Returns the state of a component, as set with replace-state or set-state.
Equivalent to (deref (r/state-atom this))"
[this]
(assert (util/reagent-component? this))
(deref (state-atom this)))
(defn replace-state
"Set state of a component."
"Set state of a component.
Equivalent to (reset! (state-atom this) new-state)"
[this new-state]
(assert (util/reagent-component? this))
(assert (or (nil? new-state) (map? new-state)))
(comp/replace-state this new-state))
(reset! (state-atom this) new-state))
(defn set-state
"Merge component state with new-state."
"Merge component state with new-state.
Equivalent to (swap! (state-atom this) merge new-state)"
[this new-state]
(assert (util/reagent-component? this))
(assert (or (nil? new-state) (map? new-state)))
(comp/set-state this new-state))
(swap! (state-atom this) merge new-state))
(defn props

View File

@ -17,17 +17,6 @@
sa
(.! this :cljsState (ratom/atom nil)))))
(defn state [this]
(deref (state-atom this)))
(defn replace-state [this new-state]
;; Don't use React's replaceState, since it doesn't play well
;; with clojure maps
(reset! (state-atom this) new-state))
(defn set-state [this new-state]
(swap! (state-atom this) merge new-state))
;; ugly circular dependency
(defn as-element [x]
(js/reagent.impl.template.as-element x))