Skip to content

Instantly share code, notes, and snippets.

@Tomyail
Created September 15, 2017 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tomyail/6aed71d4f7f3db0d8876f9d8f154b746 to your computer and use it in GitHub Desktop.
Save Tomyail/6aed71d4f7f3db0d8876f9d8f154b746 to your computer and use it in GitHub Desktop.
dynamic add reducer in redux
/**
* 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