Skip to content

Commit e2a1dd0

Browse files
committed
add initial state
1 parent a0ffda0 commit e2a1dd0

File tree

5 files changed

+20
-7
lines changed

5 files changed

+20
-7
lines changed

example/package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"react-loadable": "5.5.0",
99
"react-redux": "5.0.7",
1010
"react-scripts": "2.0.4",
11-
"redux-dynamic": "0.1.0"
11+
"redux-dynamic": "0.2.3"
1212
},
1313
"scripts": {
1414
"start": "react-scripts start",

example/src/module-two/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,26 @@ import * as actions from './actions';
22
import * as constants from './constants';
33
import * as selectors from './selectors';
44
import middleware from './middleware';
5-
import reducer from './reducer';
5+
import reducer, { initialState as state } from './reducer';
66
import Container from './container';
77

88
const reducers = {
99
[constants.STORE_KEY]: reducer
1010
};
1111

12+
const initialState = {
13+
[constants.STORE_KEY]: {
14+
...state,
15+
someParam: true
16+
}
17+
}
18+
1219
const thunkConfig = {
1320
requestTwoApi: () => new Promise(res => setTimeout(res, 2000))
1421
};
1522

1623
export {
24+
initialState,
1725
actions,
1826
constants,
1927
middleware,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redux-dynamic",
3-
"version": "0.1.1",
3+
"version": "0.2.3",
44
"description": "Allow add or remove redux modules dynamically",
55
"main": "lib/index.js",
66
"scripts": {

src/create-attach.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ import { combineReducers } from 'redux'
22
import thunkMiddleware from 'redux-thunk'
33

44
const createAttach = (data, store, dynamicMiddlewares) => ({
5+
initialState = {},
56
reducers = {},
67
thunkConfig = {},
78
middleware,
89
}) => {
9-
const nextReducers = { ...data.reducers, ...reducers }
10+
const nextReducers = Object.keys(reducers)
11+
.reduce((acc, key) => {
12+
acc[key] = (state = initialState[key], action) => reducers[key](state, action)
13+
return acc
14+
}, { ...data.reducers })
1015
store.replaceReducer(combineReducers(nextReducers))
1116

1217
const nextThunkConfig = { ...data.thunkConfig, ...thunkConfig }

0 commit comments

Comments
 (0)