diff --git a/src/reagent/core.cljs b/src/reagent/core.cljs index 635e3cb..3d04cdc 100644 --- a/src/reagent/core.cljs +++ b/src/reagent/core.cljs @@ -13,8 +13,7 @@ (def is-client util/is-client) -(defn react [] - util/react) +(def react util/react) (defn create-element "Create a native React element, by calling React.createElement directly. @@ -34,13 +33,13 @@ which is equivalent to (create-element type nil)) ([type props] (assert (not (map? props))) - ($ (react) createElement type props)) + ($ react createElement type props)) ([type props child] (assert (not (map? props))) - ($ (react) createElement type props child)) + ($ react createElement type props child)) ([type props child & children] (assert (not (map? props))) - (apply ($ (react) :createElement) type props child children))) + (apply ($ react :createElement) type props child children))) (defn as-element "Turns a vector of Hiccup syntax into a React element. Returns form unchanged if it is not a vector." diff --git a/src/reagent/dom.cljs b/src/reagent/dom.cljs index 5828182..5c5d83d 100644 --- a/src/reagent/dom.cljs +++ b/src/reagent/dom.cljs @@ -5,45 +5,38 @@ [reagent.debug :refer-macros [dbg]] [reagent.interop :refer-macros [$ $!]])) -(defonce ^:private react-dom nil) +(defonce dom (or (and (exists? js/ReactDOM) + js/ReactDOM) + (and (exists? js/require) + (js/require "react-dom")))) + +(assert dom "Could not find ReactDOM") (defonce ^:private roots (atom {})) -(defn- dom [] - (if-some [r react-dom] - r - (do - (set! react-dom - (or (and (exists? js/ReactDOM) - js/ReactDOM) - (and (exists? js/require) - (js/require "react-dom")))) - (assert react-dom "Could not find ReactDOM") - react-dom))) - (defn- unmount-comp [container] (swap! roots dissoc container) - ($ (dom) unmountComponentAtNode container)) + ($ dom unmountComponentAtNode container)) (defn- render-comp [comp container callback] (binding [util/*always-update* true] - (->> ($ (dom) render (comp) container - (fn [] - (binding [util/*always-update* false] - (swap! roots assoc container [comp container]) - (if (some? callback) - (callback)))))))) + (->> ($ dom render (comp) container + (fn [] + (binding [util/*always-update* false] + (swap! roots assoc container [comp container]) + (if (some? callback) + (callback)))))))) (defn- re-render-component [comp container] (render-comp comp container nil)) (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 element. The second argument should be a DOM node. + either a vector (using Reagent's Hiccup syntax), or a React element. The second argument should be a DOM node. -Optionally takes a callback that is called when the component is in place. + Optionally takes a callback that is called when the component is in place. -Returns the mounted component instance." + Returns the mounted component instance." ([comp container] (render comp container nil)) ([comp container callback] @@ -57,7 +50,7 @@ Returns the mounted component instance." (defn dom-node "Returns the root DOM node of a mounted component." [this] - ($ (dom) findDOMNode this)) + ($ dom findDOMNode this)) (set! tmpl/find-dom-node dom-node) diff --git a/src/reagent/dom/server.cljs b/src/reagent/dom/server.cljs index 783872d..80130c1 100644 --- a/src/reagent/dom/server.cljs +++ b/src/reagent/dom/server.cljs @@ -4,28 +4,21 @@ [reagent.impl.template :as tmpl] [reagent.interop :refer-macros [$ $!]])) -(defonce ^:private react-server nil) +(defonce server (or (and (exists? js/ReactDOMServer) + js/ReactDOMServer) + (and (exists? js/require) + (js/require "react-dom/server")))) -(defn- server [] - (if-some [r react-server] - r - (do - (set! react-server - (or (and (exists? js/ReactDOMServer) - js/ReactDOMServer) - (and (exists? js/require) - (js/require "react-dom/server")))) - (assert react-server "Could not find ReactDOMServer") - react-server))) +(assert server "Could not find ReactDOMServer") (defn render-to-string "Turns a component into an HTML string." [component] (binding [util/*non-reactive* true] - ($ (server) renderToString (tmpl/as-element component)))) + ($ server renderToString (tmpl/as-element component)))) (defn render-to-static-markup "Turns a component into an HTML string, without data-react-id attributes, etc." [component] (binding [util/*non-reactive* true] - ($ (server) renderToStaticMarkup (tmpl/as-element component)))) + ($ server renderToStaticMarkup (tmpl/as-element component))))