Skip to content

Commit

Permalink
add initial state
Browse files Browse the repository at this point in the history
  • Loading branch information
pofigizm committed Nov 5, 2018
1 parent a0ffda0 commit e2a1dd0
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
6 changes: 3 additions & 3 deletions example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 9 additions & 1 deletion example/src/module-two/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
7 changes: 6 additions & 1 deletion src/create-attach.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down

0 comments on commit e2a1dd0

Please sign in to comment.