Make sure render-to-string etc work better in a browser

Previously, they would be reactive, which could lead to all
kinds of strangeness.

Also fix pesky Closure warning.
This commit is contained in:
Dan Holmsand 2014-11-06 19:18:56 +01:00
parent 12d475fc62
commit 3f696a3922
4 changed files with 22 additions and 12 deletions

View File

@ -41,7 +41,8 @@ Returns the mounted component instance."
(defn render-to-string (defn render-to-string
"Turns a component into an HTML string." "Turns a component into an HTML string."
([component] ([component]
(.' js/React renderToString (as-component component)))) (binding [comp/*non-reactive* true]
(.' js/React renderToString (as-component component)))))
;; For backward compatibility ;; For backward compatibility
(def render-component-to-string render-to-string) (def render-component-to-string render-to-string)
@ -49,7 +50,8 @@ Returns the mounted component instance."
(defn render-to-static-markup (defn render-to-static-markup
"Turns a component into an HTML string, without data-react-id attributes, etc." "Turns a component into an HTML string, without data-react-id attributes, etc."
([component] ([component]
(.' js/React renderToStaticMarkup (as-component component)))) (binding [comp/*non-reactive* true]
(.' js/React renderToStaticMarkup (as-component component)))))
(defn ^:export force-update-all [] (defn ^:export force-update-all []
(util/force-update-all)) (util/force-update-all))

View File

@ -8,6 +8,8 @@
(declare ^:dynamic *current-component*) (declare ^:dynamic *current-component*)
(declare ^:dynamic *non-reactive*)
;;; State ;;; State
(defn state-atom [this] (defn state-atom [this]
@ -142,7 +144,7 @@
(defn add-render [fun-map render-f] (defn add-render [fun-map render-f]
(assoc fun-map (assoc fun-map
:cljsRender render-f :cljsRender render-f
:render (if util/is-client :render (if-not *non-reactive*
(fn [] (fn []
(this-as c (this-as c
(batch/run-reactively c #(do-render c)))) (batch/run-reactively c #(do-render c))))

View File

@ -233,14 +233,16 @@
([x level] ([x level]
(cond (string? x) x (cond (string? x) x
(vector? x) (vec-to-comp x level) (vector? x) (vec-to-comp x level)
(seq? x) (if-not (and (dev?) (nil? ratom/*ratom-context*)) (seq? x) (if (dev?)
(expand-seq x level) (if (nil? ratom/*ratom-context*)
(let [s (ratom/capture-derefed (expand-seq x level)
#(expand-seq x level) (let [s (ratom/capture-derefed
seq-ctx)] #(expand-seq x level)
(when (ratom/captured seq-ctx) seq-ctx)]
(warn-on-deref x)) (when (ratom/captured seq-ctx)
s)) (warn-on-deref x))
s))
(expand-seq x level))
true x))) true x)))
(defn create-class [spec] (defn create-class [spec]

View File

@ -339,4 +339,8 @@
[:div "foo"]))) [:div "foo"])))
(is (= "<div class=\"bar\"><p>foo</p></div>" (is (= "<div class=\"bar\"><p>foo</p></div>"
(reagent/render-to-static-markup (reagent/render-to-static-markup
[:div.bar [:p "foo"]])))) [:div.bar [:p "foo"]])))
(is (= "<div class=\"bar\"><p>foobar</p></div>"
(reagent/render-to-static-markup
[:div.bar {:dangerously-set-inner-HTML
{:__html "<p>foobar</p>"}} ]))))