diff --git a/src/reagent/core.cljs b/src/reagent/core.cljs index 89ef2a3..79c6e9e 100644 --- a/src/reagent/core.cljs +++ b/src/reagent/core.cljs @@ -107,27 +107,34 @@ Everything is optional, except :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 diff --git a/src/reagent/impl/component.cljs b/src/reagent/impl/component.cljs index 32efe6c..c3297f5 100644 --- a/src/reagent/impl/component.cljs +++ b/src/reagent/impl/component.cljs @@ -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)) @@ -76,7 +65,7 @@ :getInitialState (fn [] (this-as c - (set-state c (f c)))) + (reset! (state-atom c) (f c)))) :componentWillReceiveProps (fn [props]