Load React, ReactDOM etc on init

That should make error messages easier to see.
This commit is contained in:
Dan Holmsand 2015-11-20 19:15:25 +01:00
parent 443e7f45c5
commit 2a63c0a214
3 changed files with 28 additions and 43 deletions

View File

@ -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."

View File

@ -5,29 +5,22 @@
[reagent.debug :refer-macros [dbg]]
[reagent.interop :refer-macros [$ $!]]))
(defonce ^:private react-dom nil)
(defonce ^:private roots (atom {}))
(defn- dom []
(if-some [r react-dom]
r
(do
(set! react-dom
(or (and (exists? js/ReactDOM)
(defonce 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)))
(assert dom "Could not find ReactDOM")
(defonce ^:private roots (atom {}))
(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
(->> ($ dom render (comp) container
(fn []
(binding [util/*always-update* false]
(swap! roots assoc container [comp container])
@ -39,11 +32,11 @@
(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)

View File

@ -4,28 +4,21 @@
[reagent.impl.template :as tmpl]
[reagent.interop :refer-macros [$ $!]]))
(defonce ^:private react-server nil)
(defn- server []
(if-some [r react-server]
r
(do
(set! react-server
(or (and (exists? js/ReactDOMServer)
(defonce 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))))