Created
September 15, 2017 08:14
-
-
Save Tomyail/6aed71d4f7f3db0d8876f9d8f154b746 to your computer and use it in GitHub Desktop.
dynamic add reducer in redux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by lixuexin on 2017/9/15. | |
*/ | |
import { createStore, combineReducers } from 'redux'; | |
import _ from 'lodash'; | |
const reducer = combineReducers({ | |
dynamicRoot: (state = {}, action) => { | |
// if your want to change all reducer,see | |
// https://stackoverflow.com/questions/32968016/how-to-dynamically-load-reducers-for-code-splitting-in-a-redux-application | |
switch (action.type) { | |
case 'addStateNode': | |
_.set(state, action.path, action.reducer(state, action)); // dynamic change node | |
return state; | |
} | |
return state; | |
}, | |
}); | |
export function main() { | |
const store = createStore( | |
reducer, | |
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__() | |
); | |
setInterval(() => { | |
store.dispatch({ | |
type: 'addStateNode', | |
path: _.shuffle(['a', 'b', 'c']).join('.'), //create random path for test | |
reducer: combineReducers({ | |
moduleLogicReducer: (state = {}, action) => state, //your business logic | |
}), | |
}); | |
}, 2000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment