Skip to content

Instantly share code, notes, and snippets.

@lanqy
Forked from gaearon/reducers.js
Created August 27, 2016 03:57
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 lanqy/ff49b60135508c5000c6bb8061444727 to your computer and use it in GitHub Desktop.
Save lanqy/ff49b60135508c5000c6bb8061444727 to your computer and use it in GitHub Desktop.
How I'd do code splitting in Redux (pseudo code, not tested!)
import { combineReducers } from 'redux';
import users from './reducers/users';
import posts from './reducers/posts';
export default function createReducer(asyncReducers) {
return combineReducers({
users,
posts,
...asyncReducers
});
}
import { injectAsyncReducer } from './store';
function createRoutes(store) {
// ...
const CommentsRoute = {
// ...
getComponents(location, callback) {
require.ensure([
'./pages/Comments',
'./reducers/comments'
], function (require) {
let Comments = require('./pages/Comments').default;
let commentsReducer = require('./reducers/comments').default;
injectAsyncReducer(store, 'comments', commentsReducer);
callback(null, Comments);
})
}
};
// ...
}
import { createStore } from 'redux';
import createReducer from './reducers';
export default function configureStore() {
let store = createStore(createReducer());
store.asyncReducers = {};
return store;
}
export function injectAsyncReducer(store, name, asyncReducer) {
store.asyncReducers[name] = reducer;
store.replaceReducer(createReducer(store.asyncReducers));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment