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

View File

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

View File

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

View File

@ -339,4 +339,8 @@
[:div "foo"])))
(is (= "<div class=\"bar\"><p>foo</p></div>"
(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>"}} ]))))