Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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));
}
@HaNdTriX
Copy link

HaNdTriX commented Mar 19, 2017

Just in case someone is interested.
I have implemented a running ssr example for next.js.

vercel/next.js#1459

@cvbuelow
Copy link

cvbuelow commented Sep 30, 2017

Here's a solution for injecting the initialState in async reducers, suggested by @gaearon
https://gist.github.com/cvbuelow/ea7bd4deb7824fdb65d81cd2b8c60f5e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment