Revert adding of state-atom

There may be a better api - and it's not very important anyway.
This commit is contained in:
Dan Holmsand 2015-02-04 22:50:22 +01:00
parent 017fa9c106
commit 721e3bab00
2 changed files with 18 additions and 14 deletions

View File

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

View File

@ -17,6 +17,17 @@
sa sa
(.! this :cljsState (ratom/atom nil))))) (.! 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 ;; ugly circular dependency
(defn as-element [x] (defn as-element [x]
(js/reagent.impl.template.as-element x)) (js/reagent.impl.template.as-element x))