diff --git a/example/package-lock.json b/example/package-lock.json index ff69fd8..276e963 100644 --- a/example/package-lock.json +++ b/example/package-lock.json @@ -11684,9 +11684,9 @@ } }, "redux-dynamic": { - "version": "0.1.0", - "resolved": "https://registry.npmjs.org/redux-dynamic/-/redux-dynamic-0.1.0.tgz", - "integrity": "sha512-pn4bINzQN7QSZ28y7qUgzqUUyvCxYh2XLIMkwoWJrdeCAeHFVrapnirsS0YRirHoyZCb/PyqcDosMyDdpULg1Q==", + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/redux-dynamic/-/redux-dynamic-0.2.3.tgz", + "integrity": "sha512-8+V6xRLpFKaKL7OeN9TAdRJCxXNXOLbLTkVbkzEnH0ABoleThG3wlcNoiuoXxDybx6oGizQQfR0OYmo/iBPFrA==", "requires": { "redux": ">=3.0.0", "redux-dynamic-middlewares": ">=1.0.0", diff --git a/example/package.json b/example/package.json index 552a84c..33874db 100644 --- a/example/package.json +++ b/example/package.json @@ -8,7 +8,7 @@ "react-loadable": "5.5.0", "react-redux": "5.0.7", "react-scripts": "2.0.4", - "redux-dynamic": "0.1.0" + "redux-dynamic": "0.2.3" }, "scripts": { "start": "react-scripts start", diff --git a/example/src/module-two/index.js b/example/src/module-two/index.js index 32b54f5..40ef101 100644 --- a/example/src/module-two/index.js +++ b/example/src/module-two/index.js @@ -2,18 +2,26 @@ import * as actions from './actions'; import * as constants from './constants'; import * as selectors from './selectors'; import middleware from './middleware'; -import reducer from './reducer'; +import reducer, { initialState as state } from './reducer'; import Container from './container'; const reducers = { [constants.STORE_KEY]: reducer }; +const initialState = { + [constants.STORE_KEY]: { + ...state, + someParam: true + } +} + const thunkConfig = { requestTwoApi: () => new Promise(res => setTimeout(res, 2000)) }; export { + initialState, actions, constants, middleware, diff --git a/package.json b/package.json index 94b5cef..f4b8a46 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "redux-dynamic", - "version": "0.1.1", + "version": "0.2.3", "description": "Allow add or remove redux modules dynamically", "main": "lib/index.js", "scripts": { diff --git a/src/create-attach.js b/src/create-attach.js index 38d2161..2c62a5a 100644 --- a/src/create-attach.js +++ b/src/create-attach.js @@ -2,11 +2,16 @@ import { combineReducers } from 'redux' import thunkMiddleware from 'redux-thunk' const createAttach = (data, store, dynamicMiddlewares) => ({ + initialState = {}, reducers = {}, thunkConfig = {}, middleware, }) => { - const nextReducers = { ...data.reducers, ...reducers } + const nextReducers = Object.keys(reducers) + .reduce((acc, key) => { + acc[key] = (state = initialState[key], action) => reducers[key](state, action) + return acc + }, { ...data.reducers }) store.replaceReducer(combineReducers(nextReducers)) const nextThunkConfig = { ...data.thunkConfig, ...thunkConfig }