-
Notifications
You must be signed in to change notification settings - Fork 0
/
configureStore.js
50 lines (40 loc) · 1.41 KB
/
configureStore.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
import { createStore, compose, applyMiddleware } from "redux";
import { persistStore, persistCombineReducers } from "redux-persist";
import storage from "redux-persist/es/storage"; // default: localStorage if web, AsyncStorage if react-native
import { createLogger } from "redux-logger";
import createSagaMiddleware from "redux-saga";
import rootReducers from "app/common/reducers"; // where reducers is a object of reducers
import sagas from "app/common/sagas";
const config = {
key: "root",
storage,
blacklist: ["default", "loadingReducer"],
debug: true //to get useful logging
};
const middleware = [];
const sagaMiddleware = createSagaMiddleware();
middleware.push(sagaMiddleware);
if (__DEV__) {
middleware.push(createLogger());
const freeze = require('redux-freeze')
middleware.push(freeze)
}
const reducers = persistCombineReducers(config, rootReducers);
const clearState = (reducer) => {
return function (state, action) {
if (action.type === 'RESET_STATE') {
state = undefined;
}
return reducer(state, action);
};
}
const enhancers = [applyMiddleware(...middleware)];
const persistConfig = { enhancers };
const store = createStore(clearState(reducers), undefined, compose(...enhancers));
const persistor = persistStore(store, persistConfig, () => {
});
const configureStore = () => {
return { persistor, store };
};
sagaMiddleware.run(sagas);
export default configureStore;