Introduce render, render-to-string and render-to-static-markup

The new names correspond to changes in React in 0.12.0.

Both render-component and render-component-to-string are kept for
backward compatibility.

Call React.render etc to avoid deprecation warnings from React.
This commit is contained in:
Dan Holmsand 2014-11-06 11:34:51 +01:00
parent b491098d5e
commit 12d475fc62
4 changed files with 26 additions and 7 deletions

View File

@ -1,5 +1,5 @@
(defproject reagent "0.4.3"
(defproject reagent "0.4.4-SNAPSHOT"
:url "http://github.com/holmsand/reagent"
:license {:name "MIT"}
:description "A simple ClojureScript interface to React"

View File

@ -16,7 +16,7 @@
[form]
(tmpl/as-component form))
(defn render-component
(defn render
"Render a Reagent component into the DOM. The first argument may be either a
vector (using Reagent's Hiccup syntax), or a React component. The second argument should be a DOM node.
@ -24,21 +24,32 @@ Optionally takes a callback that is called when the component is in place.
Returns the mounted component instance."
([comp container]
(render-component comp container nil))
(render comp container nil))
([comp container callback]
(let [f (fn []
(as-component (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]
(util/unmount-component-at-node container))
(defn render-component-to-string
(defn render-to-string
"Turns a component into an HTML string."
([component]
(.' js/React renderComponentToString (as-component component))))
(.' js/React renderToString (as-component component))))
;; For backward compatibility
(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]
(.' js/React renderToStaticMarkup (as-component component))))
(defn ^:export force-update-all []
(util/force-update-all))

View File

@ -126,7 +126,7 @@
(defn re-render-component [comp container]
(try
(.' js/React renderComponent (comp) container)
(.' js/React render (comp) container)
(catch js/Object e
(do
(try
@ -139,7 +139,7 @@
(throw e)))))
(defn render-component [comp container callback]
(.' js/React renderComponent (comp) container
(.' js/React render (comp) container
(fn []
(let [id (get-root-id container)]
(when-not (nil? id)

View File

@ -332,3 +332,11 @@
(as-string [null-comp false]))))
(is (re-find #"test-null-component"
(as-string [null-comp true])))))
(deftest test-static-markup
(is (= "<div>foo</div>"
(reagent/render-to-static-markup
[:div "foo"])))
(is (= "<div class=\"bar\"><p>foo</p></div>"
(reagent/render-to-static-markup
[:div.bar [:p "foo"]]))))