-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstore.js
51 lines (43 loc) · 1.29 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
50
import { createStore, combineReducers, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import * as staticReducers from 'modules';
import { createWrapper, HYDRATE } from 'next-redux-wrapper';
function createReducer(asyncReducers) {
const combinedReducers = combineReducers({
...staticReducers,
...asyncReducers
});
const allReducers = (state, action) => {
if (action.type === HYDRATE) {
const nextState = {
...state,
...action.payload,
};
return nextState;
}
else {
return combinedReducers(state, action);
}
}
return allReducers;
}
const makeStore = (context) => {
const store = createStore(
createReducer(),
{},
compose(
applyMiddleware(thunk),
/* Redux dev tool, install chrome extension in
* https://chrome.google.com/webstore/detail/redux-devtools/lmhkpmbekcpmknklioeibfkpmmfibljd?hl=en */
typeof window === 'object' &&
typeof window.devToolsExtension !== 'undefined' ? window.devToolsExtension() : f => f
)
);
store.asyncReducers = {};
store.injectReducer = (key, asyncReducer) => {
store.asyncReducers[key] = asyncReducer;
store.replaceReducer(createReducer(store.asyncReducers));
}
return store;
}
export default createWrapper(makeStore);