diff --git a/src/reagent/core.cljs b/src/reagent/core.cljs index 396a42d..a9d6e8e 100644 --- a/src/reagent/core.cljs +++ b/src/reagent/core.cljs @@ -64,33 +64,39 @@ Everything is optional, except :render. (defn state "Returns the state of a component, as set with replace-state or set-state." [this] + (assert (util/reagent-component? this)) (comp/state this)) (defn replace-state "Set state of a component." [this new-state] + (assert (util/reagent-component? this)) (comp/replace-state this new-state)) (defn set-state "Merge component state with new-state." [this new-state] + (assert (util/reagent-component? this)) (comp/set-state this new-state)) (defn props "Returns the props passed to a component." [this] - (comp/get-props this)) + (assert (util/reagent-component? this)) + (util/get-props this)) (defn children "Returns the children passed to a component." [this] - (comp/get-children this)) + (assert (util/reagent-component? this)) + (util/get-children this)) (defn argv "Returns the entire Hiccup form passed to the component." [this] - (comp/get-argv this)) + (assert (util/reagent-component? this)) + (util/get-argv this)) (defn dom-node "Returns the root DOM node of a mounted component." diff --git a/src/reagent/impl/component.cljs b/src/reagent/impl/component.cljs index 457750d..4dae8d2 100644 --- a/src/reagent/impl/component.cljs +++ b/src/reagent/impl/component.cljs @@ -10,7 +10,7 @@ (def cljs-state "cljsState") (def cljs-render "cljsRender") -;;; Accessors +;;; State (defn state [this] (aget this cljs-state)) @@ -26,37 +26,14 @@ (defn set-state [this new-state] (replace-state this (merge (state this) new-state))) -(defn js-props [C] - (aget C "props")) - -(defn extract-props [v] - (let [p (get v 1)] - (if (map? p) p))) - -(defn extract-children [v] - (let [p (get v 1) - first-child (if (or (nil? p) (map? p)) 2 1)] - (if (> (count v) first-child) - (subvec v first-child)))) - -(defn get-argv [C] - (-> C js-props (aget cljs-argv))) - -(defn get-props [C] - (-> C get-argv extract-props)) - -(defn get-children [C] - (-> C get-argv extract-children)) - ;;; Rendering - (defn do-render [C] (binding [*current-component* C] (let [f (aget C cljs-render) _ (assert (ifn? f)) - p (js-props C) + p (util/js-props C) res (if (nil? (aget C "componentFunction")) (f C) (let [argv (aget p cljs-argv) @@ -77,7 +54,7 @@ res))))) -;;; Function wrapping +;;; Method wrapping (defn custom-wrapper [key f] (case key @@ -100,7 +77,7 @@ (this-as C ;; Don't care about nextstate here, we use forceUpdate ;; when only when state has changed anyway. - (let [inprops (js-props C) + (let [inprops (util/js-props C) old-argv (aget inprops cljs-argv) new-argv (aget nextprops cljs-argv)] (if (nil? f) diff --git a/src/reagent/impl/template.cljs b/src/reagent/impl/template.cljs index 9be0f30..26804e4 100644 --- a/src/reagent/impl/template.cljs +++ b/src/reagent/impl/template.cljs @@ -43,13 +43,6 @@ (coll? v) (clj->js v) :else (fn [& args] (apply v args))))) -(defn extract-props [v] - (let [p (get v 1)] - (if (map? p) p))) - -(defn get-props [this] - (-> this (aget "props") (aget cljs-argv) extract-props)) - (defn undash-prop-name [n] (or (attr-aliases n) (util/dash-to-camel n))) @@ -94,12 +87,12 @@ ;;; Specialization for input components (defn input-initial-state [this] - (let [props (get-props this)] + (let [props (util/get-props this)] #js {:value (:value props) :checked (:checked props)})) (defn input-handle-change [this e] - (let [props (get-props this) + (let [props (util/get-props this) on-change (or (props :on-change) (props "onChange"))] (when-not (nil? on-change) (let [target (.-target e)] @@ -108,7 +101,7 @@ (on-change e)))) (defn input-will-receive-props [this new-props] - (let [props (-> new-props (aget cljs-argv) extract-props)] + (let [props (-> new-props (aget cljs-argv) util/extract-props)] (.setState this #js {:value (:value props) :checked (:checked props)}))) @@ -127,7 +120,7 @@ (declare as-component) (defn wrapped-render [this comp id-class input-setup] - (let [inprops (aget this "props") + (let [inprops (util/js-props this) argv (aget inprops cljs-argv) props (get argv 1) hasprops (or (nil? props) (map? props)) @@ -144,7 +137,7 @@ (.apply comp nil jsargs))) (defn wrapped-should-update [C nextprops nextstate] - (let [inprops (aget C "props") + (let [inprops (util/js-props C) a1 (aget inprops cljs-argv) a2 (aget nextprops cljs-argv)] (not (util/equal-args a1 a2)))) diff --git a/src/reagent/impl/util.cljs b/src/reagent/impl/util.cljs index b7b4424..91bc2ae 100644 --- a/src/reagent/impl/util.cljs +++ b/src/reagent/impl/util.cljs @@ -6,9 +6,38 @@ (def is-client (not (nil? (try (.-document js/window) (catch js/Object e nil))))) +(def React reactimport/React) + +;;; Props accessors + +(def props "props") (def cljs-level "cljsLevel") (def cljs-argv "cljsArgv") -(def React reactimport/React) + +(defn js-props [C] + (aget C props)) + +(defn extract-props [v] + (let [p (get v 1)] + (if (map? p) p))) + +(defn extract-children [v] + (let [p (get v 1) + first-child (if (or (nil? p) (map? p)) 2 1)] + (if (> (count v) first-child) + (subvec v first-child)))) + +(defn get-argv [C] + (-> C (aget props) (aget cljs-argv))) + +(defn get-props [C] + (-> C (aget props) (aget cljs-argv) extract-props)) + +(defn get-children [C] + (-> C (aget props) (aget cljs-argv) extract-children)) + +(defn reagent-component? [C] + (-> C get-argv nil? not)) ;; Misc utilities