spee.ch/client/index.js

46 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-01-29 20:47:12 +01:00
import React from 'react';
2018-02-21 22:42:41 +01:00
import { hydrate } from 'react-dom';
import { Provider } from 'react-redux';
2018-02-07 04:00:52 +01:00
import { createStore, applyMiddleware, compose } from 'redux';
import { BrowserRouter } from 'react-router-dom';
2018-01-29 20:47:12 +01:00
import Reducer from 'reducers';
2018-02-07 04:00:52 +01:00
import createSagaMiddleware from 'redux-saga';
import rootSaga from 'sagas';
import GAListener from 'components/GAListener';
import App from './app';
2018-01-29 20:47:12 +01:00
// get the state from a global variable injected into the server-generated HTML
const preloadedState = window.__PRELOADED_STATE__ || null;
// Allow the passed state to be garbage-collected
delete window.__PRELOADED_STATE__;
// create and apply middleware
2018-02-07 04:00:52 +01:00
const sagaMiddleware = createSagaMiddleware();
const middleware = applyMiddleware(sagaMiddleware);
const reduxMiddleware = window.__REDUX_DEVTOOLS_EXTENSION__ ? compose(middleware, window.__REDUX_DEVTOOLS_EXTENSION__()) : middleware;
2018-02-07 04:00:52 +01:00
// create teh store
let store;
if (preloadedState) {
store = createStore(Reducer, preloadedState, reduxMiddleware);
} else {
store = createStore(Reducer, reduxMiddleware);
}
// run the saga middlweare
sagaMiddleware.run(rootSaga);
// render the app
hydrate(
<Provider store={store}>
<BrowserRouter>
<GAListener>
<App />
</GAListener>
</BrowserRouter>
</Provider>,
document.getElementById('react-app')
);