mirror of https://github.com/status-im/reagent.git
Load React, ReactDOM etc on init
That should make error messages easier to see.
This commit is contained in:
parent
443e7f45c5
commit
2a63c0a214
|
@ -13,8 +13,7 @@
|
||||||
|
|
||||||
(def is-client util/is-client)
|
(def is-client util/is-client)
|
||||||
|
|
||||||
(defn react []
|
(def react util/react)
|
||||||
util/react)
|
|
||||||
|
|
||||||
(defn create-element
|
(defn create-element
|
||||||
"Create a native React element, by calling React.createElement directly.
|
"Create a native React element, by calling React.createElement directly.
|
||||||
|
@ -34,13 +33,13 @@ which is equivalent to
|
||||||
(create-element type nil))
|
(create-element type nil))
|
||||||
([type props]
|
([type props]
|
||||||
(assert (not (map? props)))
|
(assert (not (map? props)))
|
||||||
($ (react) createElement type props))
|
($ react createElement type props))
|
||||||
([type props child]
|
([type props child]
|
||||||
(assert (not (map? props)))
|
(assert (not (map? props)))
|
||||||
($ (react) createElement type props child))
|
($ react createElement type props child))
|
||||||
([type props child & children]
|
([type props child & children]
|
||||||
(assert (not (map? props)))
|
(assert (not (map? props)))
|
||||||
(apply ($ (react) :createElement) type props child children)))
|
(apply ($ react :createElement) type props child children)))
|
||||||
|
|
||||||
(defn as-element
|
(defn as-element
|
||||||
"Turns a vector of Hiccup syntax into a React element. Returns form unchanged if it is not a vector."
|
"Turns a vector of Hiccup syntax into a React element. Returns form unchanged if it is not a vector."
|
||||||
|
|
|
@ -5,45 +5,38 @@
|
||||||
[reagent.debug :refer-macros [dbg]]
|
[reagent.debug :refer-macros [dbg]]
|
||||||
[reagent.interop :refer-macros [$ $!]]))
|
[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 {}))
|
(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]
|
(defn- unmount-comp [container]
|
||||||
(swap! roots dissoc container)
|
(swap! roots dissoc container)
|
||||||
($ (dom) unmountComponentAtNode container))
|
($ dom unmountComponentAtNode container))
|
||||||
|
|
||||||
(defn- render-comp [comp container callback]
|
(defn- render-comp [comp container callback]
|
||||||
(binding [util/*always-update* true]
|
(binding [util/*always-update* true]
|
||||||
(->> ($ (dom) render (comp) container
|
(->> ($ dom render (comp) container
|
||||||
(fn []
|
(fn []
|
||||||
(binding [util/*always-update* false]
|
(binding [util/*always-update* false]
|
||||||
(swap! roots assoc container [comp container])
|
(swap! roots assoc container [comp container])
|
||||||
(if (some? callback)
|
(if (some? callback)
|
||||||
(callback))))))))
|
(callback))))))))
|
||||||
|
|
||||||
(defn- re-render-component [comp container]
|
(defn- re-render-component [comp container]
|
||||||
(render-comp comp container nil))
|
(render-comp comp container nil))
|
||||||
|
|
||||||
(defn render
|
(defn render
|
||||||
"Render a Reagent component into the DOM. The first argument may be
|
"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]
|
([comp container]
|
||||||
(render comp container nil))
|
(render comp container nil))
|
||||||
([comp container callback]
|
([comp container callback]
|
||||||
|
@ -57,7 +50,7 @@ Returns the mounted component instance."
|
||||||
(defn dom-node
|
(defn dom-node
|
||||||
"Returns the root DOM node of a mounted component."
|
"Returns the root DOM node of a mounted component."
|
||||||
[this]
|
[this]
|
||||||
($ (dom) findDOMNode this))
|
($ dom findDOMNode this))
|
||||||
|
|
||||||
(set! tmpl/find-dom-node dom-node)
|
(set! tmpl/find-dom-node dom-node)
|
||||||
|
|
||||||
|
|
|
@ -4,28 +4,21 @@
|
||||||
[reagent.impl.template :as tmpl]
|
[reagent.impl.template :as tmpl]
|
||||||
[reagent.interop :refer-macros [$ $!]]))
|
[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 []
|
(assert server "Could not find ReactDOMServer")
|
||||||
(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)))
|
|
||||||
|
|
||||||
(defn render-to-string
|
(defn render-to-string
|
||||||
"Turns a component into an HTML string."
|
"Turns a component into an HTML string."
|
||||||
[component]
|
[component]
|
||||||
(binding [util/*non-reactive* true]
|
(binding [util/*non-reactive* true]
|
||||||
($ (server) renderToString (tmpl/as-element component))))
|
($ server renderToString (tmpl/as-element component))))
|
||||||
|
|
||||||
(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]
|
||||||
(binding [util/*non-reactive* true]
|
(binding [util/*non-reactive* true]
|
||||||
($ (server) renderToStaticMarkup (tmpl/as-element component))))
|
($ server renderToStaticMarkup (tmpl/as-element component))))
|
||||||
|
|
Loading…
Reference in New Issue