forked from goatslacker/alt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIsomorphicRenderer.js
48 lines (46 loc) · 1.07 KB
/
IsomorphicRenderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* IsomorphicRenderer(alt: AltInstance, App: ReactElement): mixed
*
* > The glue that it takes to render a react element isomorphically
*
* ** This util depends on iso and react **
*
* Usage:
*
* ```js
* var IsomorphicRenderer = require('alt/utils/IsomorphicRenderer');
* var React = require('react');
* var Alt = require('alt');
* var alt = new Alt();
*
* var App = React.createClass({
* render() {
* return (
* <div>Hello World</div>
* );
* }
* });
*
* module.exports = IsomorphicRenderer(alt, App);
* ```
*/
import Iso from 'iso'
import React from 'react'
export default function IsomorphicRenderer(alt, App) {
/*eslint-disable */
if (typeof window === 'undefined') {
/*eslint-enable */
return () => {
const app = React.renderToString(React.createElement(App))
const markup = Iso.render(app, alt.takeSnapshot())
alt.flush()
return markup
}
} else {
Iso.bootstrap((state, _, node) => {
const app = React.createElement(App)
alt.bootstrap(state)
React.render(app, node)
})
}
}