forked from noriaki/hypernova-react-redux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.jsx
40 lines (37 loc) · 1.14 KB
/
index.jsx
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
import React from 'react';
import ReactDOM from 'react-dom';
import ReactDOMServer from 'react-dom/server';
import { Provider } from 'react-redux';
import hypernova, { serialize, load } from 'hypernova';
function buildProvider(connectedComponent, store) {
return (
<Provider store={store}>
{
React.isValidElement(connectedComponent)
? connectedComponent
: React.createElement(connectedComponent)
}
</Provider>
);
}
export const renderReactRedux =
(name, connectedComponent, configureStore) => hypernova({
server() {
return (props) => {
const provider = buildProvider(connectedComponent, configureStore(props));
const contents = ReactDOMServer.renderToString(provider);
return serialize(name, contents, props);
};
},
client() {
const payloads = load(name);
if (payloads) {
payloads.forEach((payload) => {
const { node, data } = payload;
const provider = buildProvider(connectedComponent, configureStore(data));
ReactDOM.render(provider, node);
});
}
return connectedComponent;
}
});