Skip to content

Instantly share code, notes, and snippets.

@pedronauck
Created February 2, 2017 14:45
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pedronauck/7762444741b041cbe4b0d57c5ef18338 to your computer and use it in GitHub Desktop.
Save pedronauck/7762444741b041cbe4b0d57c5ef18338 to your computer and use it in GitHub Desktop.
import { combineReducers } from 'redux';
// Define the context to search files
const CONTEXT = require.context('./', true, /\.\/(.*)\/index.js?$/i);
// Populate a object with the reducer
const importReducer = (req) => (obj, path) => {
const [, componentName] = path.match(/\.\/(.*)\/index.js?$/i);
const reducer = {
[componentName]: req(path).default
};
return Object.assign({}, obj, reducer);
};
// Generate a object with all reducers
const getReducers = (ctx) =>
ctx.keys().reduce(importReducer(ctx), {});
// Export my root reducer
export default combineReducers(getReducers(CONTEXT));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment