/
store.js
49 lines (45 loc) · 1.89 KB
/
store.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
/* Create the store with asynchronously loaded reducers */
import { createStore, applyMiddleware, compose } from 'redux';
import { fromJS, Iterable } from 'immutable';
import { routerMiddleware } from 'react-router-redux';
import { combineReducers } from 'redux-immutable';
import { captureError } from 'utils/errorReporting';
import { withInjectableReducers } from 'utils/ReducerInjector';
import { withSaga } from 'utils/SagaInjector';
export default function configureStore(initialState = {}, history) {
// routerMiddleware: Syncs the location/URL path to the state
const middlewares = [
routerMiddleware(history),
];
// log redux state in development
if (process.env.NODE_ENV === 'development') {
const stateTransformer = (state) => (
// pure JS is easier to read than Immutable objects
Iterable.isIterable(state) ? state.toJS() : state
);
const { createLogger } = require('redux-logger'); // eslint-disable-line global-require
middlewares.push(createLogger({ stateTransformer }));
}
// If Redux DevTools Extension is installed use it, otherwise use Redux compose
/* eslint-disable no-underscore-dangle */
const composeEnhancers =
process.env.NODE_ENV !== 'production' &&
typeof window === 'object' &&
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
// TODO Try to remove when `react-router-redux` is out of beta, LOCATION_CHANGE should not be fired more than once after hot reloading
// Prevent recomputing reducers for `replaceReducer`
shouldHotReload: false,
})
: compose;
/* eslint-enable */
return createStore(
(state) => state, // noop reducer, the actual ones will be injected
fromJS(initialState),
composeEnhancers(
withSaga({ onError: captureError }),
applyMiddleware(...middlewares),
withInjectableReducers(combineReducers),
)
);
}