/
store.js
46 lines (37 loc) · 1.32 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
/* global module, require */
import {createStore, applyMiddleware} from 'redux';
import thunk from 'redux-thunk';
import reducer from './reducers/reducer';
let store;
/**
* Initialize a Redux store with thunk, plus logging (only in development mode) middleware
*
* @returns {Store<GenericStoreEnhancer>}
* An initialized redux store with middleware and possible hot reloading of reducers
*/
const initializeStore = () => {
if (store) {
return store;
}
// only attach logger to middleware in non-production mode
store =
process.env.NODE_ENV === 'production' // eslint-disable-line no-process-env
? createStore(reducer, applyMiddleware(thunk))
: createStore(
reducer,
window.__REDUX_DEVTOOLS_EXTENSION__ &&
window.__REDUX_DEVTOOLS_EXTENSION__(),
applyMiddleware(thunk)
);
// TODO - Protect this under a debug mode?
window.store = store; /* global window:true */
if (module.hot) {
// Enable hot module replacement for reducers
module.hot.accept('./reducers/reducer', () => {
const nextRootReducer = require('./reducers/reducer');
store.replaceReducer(nextRootReducer);
});
}
return store;
};
export default initializeStore;