-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
61 lines (52 loc) · 1.82 KB
/
index.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
49
50
51
52
53
54
55
56
57
58
59
60
61
import './styles/global.scss';
import "babel-polyfill";
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter, Route, Link } from 'react-router-dom';
import { createStore, combineReducers, applyMiddleware } from 'redux';
import { Provider } from 'react-redux';
import createHistory from 'history/createBrowserHistory';
import { ConnectedRouter, routerReducer, routerMiddleware } from 'react-router-redux';
import { renderRoutes } from 'react-router-config';
import thunkMiddleware from 'redux-thunk';
import Loadable from 'react-loadable';
import App from '../universal/components/app';
import { pageData, dataLoading } from '../universal/reducers';
import routes from '../universal/routes';
import * as types from '../universal/actions/types';
const history = createHistory();
const middleware = routerMiddleware(history);
const initialState = window.__PRELOADED_STATE__;
// App reducer pattern is needed so state can be rehydrated
// when it receives new data after a history event
// https://stackoverflow.com/a/35641992/392572
// TODO: share this between client and server
const appReducer = combineReducers({
pageData,
dataLoading,
routing: routerReducer
});
const rootReducer = (state, action) => {
if (action.type === types.REHYDRATE_STATE) {
state = { ...state, ...action.data }
}
return appReducer(state, action);
}
// TODO: create a store factory that can be used to create a store on the client
// and server
const store = createStore(
rootReducer,
initialState,
applyMiddleware(middleware, thunkMiddleware)
);
window.main = () => {
Loadable.preloadReady().then(() => {
ReactDOM.hydrate((
<Provider store={store}>
<ConnectedRouter history={history}>
{renderRoutes(routes)}
</ConnectedRouter>
</Provider>
), document.getElementById('app'))
});
}