as-component -> as-element

This commit is contained in:
Dan Holmsand 2014-12-02 11:17:07 +01:00
parent b287b32860
commit a48e70c4de
3 changed files with 15 additions and 16 deletions

View File

@ -11,10 +11,10 @@
(def is-client util/is-client)
(defn as-component
(defn as-element
"Turns a vector of Hiccup syntax into a React component. Returns form unchanged if it is not a vector."
[form]
(tmpl/as-component form))
(tmpl/as-element form))
(defn render
"Render a Reagent component into the DOM. The first argument may be either a
@ -27,12 +27,9 @@ Returns the mounted component instance."
(render comp container nil))
([comp container callback]
(let [f (fn []
(as-component (if (fn? comp) (comp) comp)))]
(as-element (if (fn? comp) (comp) comp)))]
(util/render-component f container callback))))
;; For backward compatibility
(def render-component render)
(defn unmount-component-at-node
"Remove a component from the given DOM node."
[container]
@ -42,16 +39,18 @@ Returns the mounted component instance."
"Turns a component into an HTML string."
([component]
(binding [comp/*non-reactive* true]
(.' js/React renderToString (as-component component)))))
(.' js/React renderToString (as-element component)))))
;; For backward compatibility
(def as-component as-element)
(def render-component render)
(def render-component-to-string render-to-string)
(defn render-to-static-markup
"Turns a component into an HTML string, without data-react-id attributes, etc."
([component]
(binding [comp/*non-reactive* true]
(.' js/React renderToStaticMarkup (as-component component)))))
(.' js/React renderToStaticMarkup (as-element component)))))
(defn ^:export force-update-all []
(util/force-update-all))

View File

@ -30,7 +30,7 @@
(swap! (state-atom this) merge new-state))
;; set from template, to avoid circular deps
(def as-component nil)
(def as-element nil)
;;; Rendering
@ -51,7 +51,7 @@
5 (f (nth argv 1) (nth argv 2) (nth argv 3) (nth argv 4))
(apply f (subvec argv 1)))))]
(if (vector? res)
(as-component res)
(as-element res)
(if (ifn? res)
(do
(.! c :cljsRender res)
@ -199,7 +199,7 @@
(let [spec (cljsify body)
res (.' js/React createClass spec)
f (fn [& args]
(as-component (apply vector res args)))]
(as-element (apply vector res args)))]
(util/cache-react-class f res)
(util/cache-react-class res res)
f))

View File

@ -212,7 +212,7 @@
(declare expand-seq)
(defn as-component [x]
(defn as-element [x]
(cond (string? x) x
(vector? x) (vec-to-comp x)
(seq? x) (if (dev?)
@ -228,22 +228,22 @@
true x))
;; Cheat, to avoid ugly circular dependency
(set! reagent.impl.component/as-component as-component)
(set! reagent.impl.component/as-element as-element)
(defn expand-seq [s]
(let [a (into-array s)]
(dotimes [i (alength a)]
(aset a i (as-component (aget a i))))
(aset a i (as-element (aget a i))))
a))
(defn make-element [argv comp jsprops first-child]
(if (== (count argv) (inc first-child))
;; Optimize common case of one child
(.' js/React createElement comp jsprops
(as-component (nth argv first-child)))
(as-element (nth argv first-child)))
(.apply (.' js/React :createElement) nil
(reduce-kv (fn [a k v]
(when (>= k first-child)
(.push a (as-component v)))
(.push a (as-element v)))
a)
#js[comp jsprops] argv))))