Skip to content

feature request: make combineReducers recursive #2022

Closed
@jimbolla

Description

@jimbolla

Currently, building a reducer with combineReducers looks something like this:

const reducer = combineReducers({
  form,
  entities: combineReducers({
    customers: combineReducers({
      // ...
    }),
    products: combineReducers({
      // ...
    }),
    orders: combineReducers({
      // ...
    }),
    // ...
  }),
  ui: combineReducers({
    // ...
  })
});

... and it would be nice to be able to do this:

const reducer = combineReducers({
  form,
  entities: {
    customers: {
      // ...
    },
    products: {
      // ...
    ),
    orders: {
      // ...
    ),
    // ...
  }),
  ui: {
    // ...
  }
});

This would reduce some boilerplate.

Implementation should be rather trivial:

 src/combineReducers.js | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/src/combineReducers.js b/src/combineReducers.js
index f0ff7eb..04693a2 100644
--- a/src/combineReducers.js
+++ b/src/combineReducers.js
@@ -111,6 +111,8 @@ export default function combineReducers(reducers) {

     if (typeof reducers[key] === 'function') {
       finalReducers[key] = reducers[key]
+    } else if (typeof reducers[key] === 'object' && reducers[key]) {
+      finalReducers[key] = combineReducers(reducers[key])
     }
   }
   var finalReducerKeys = Object.keys(finalReducers)

I can create test(s) + PR for this if you think it's a good idea.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions