javascriptfluxreduxcode-splitting

How to dynamically load reducers for code splitting in a Redux application?


I'm going migrate to Redux.

My application consists of a lot of parts (pages, components) so I want to create many reducers. Redux examples show that I should use combineReducers() to generate one reducer.

Also as I understand Redux application should have one store and it is created once the application starts. When the store is being created I should pass my combined reducer. This makes sense if the application is not too big.

But what if I build more than one JavaScript bundle? For example, each page of application has own bundle. I think in this case the one combined reducer is not good. I looked through the sources of Redux and I have found replaceReducer() function. It seems to be what I want.

I could create combined reducer for each part my application and use replaceReducer() when I move between parts of application.

Is this a good approach?


Solution

  • Update: see also how Twitter does it.

    This is not a full answer but should help you get started. Note that I'm not throwing away old reducers—I'm just adding new ones to the combination list. I see no reason to throw away the old reducers—even in the largest app you're unlikely to have thousands of dynamic modules, which is the point where you might want to disconnect some reducers in your application.

    reducers.js

    import { combineReducers } from 'redux';
    import users from './reducers/users';
    import posts from './reducers/posts';
    
    export default function createReducer(asyncReducers) {
      return combineReducers({
        users,
        posts,
        ...asyncReducers
      });
    }
    

    store.js

    import { createStore } from 'redux';
    import createReducer from './reducers';
    
    export default function configureStore(initialState) {
      const store = createStore(createReducer(), initialState);
      store.asyncReducers = {};
      return store;
    }
    
    export function injectAsyncReducer(store, name, asyncReducer) {
      store.asyncReducers[name] = asyncReducer;
      store.replaceReducer(createReducer(store.asyncReducers));
    }
    

    routes.js

    import { injectAsyncReducer } from './store';
    
    // Assuming React Router here but the principle is the same
    // regardless of the library: make sure store is available
    // when you want to require.ensure() your reducer so you can call
    // injectAsyncReducer(store, name, reducer).
    
    function createRoutes(store) {
      // ...
    
      const CommentsRoute = {
        // ...
    
        getComponents(location, callback) {
          require.ensure([
            './pages/Comments',
            './reducers/comments'
          ], function (require) {
            const Comments = require('./pages/Comments').default;
            const commentsReducer = require('./reducers/comments').default;
    
            injectAsyncReducer(store, 'comments', commentsReducer);
            callback(null, Comments);
          })
        }
      };
    
      // ...
    }
    

    There may be neater way of expressing this—I'm just showing the idea.