Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature request: allow bindActionCreators to accept array #1798

Closed
jimbolla opened this issue Jun 10, 2016 · 3 comments
Closed

feature request: allow bindActionCreators to accept array #1798

jimbolla opened this issue Jun 10, 2016 · 3 comments

Comments

@jimbolla
Copy link
Contributor

jimbolla commented Jun 10, 2016

Currently, bindActionCreators accepts a single function or an object of functions. It would be useful if it accepted an array as well, to aid this code pattern:

import * as ac from './actionCreators';

// later in file...
const [doThis, doSomethingElse, doAnotherThing] = bindActionCreators(
  [ac.doThis, ac.doSomethingElse, ac.doAnotherThing
], dispatch);
// ...

I like to specify my creator names explicitly because any mistakes are caught immediately by eslint-plugin-import/namespace as an error.

I believe this is the necessary change:

diff --git a/src/bindActionCreators.js b/src/bindActionCreators.js
index e70e270..6050c32 100644
--- a/src/bindActionCreators.js
+++ b/src/bindActionCreators.js
@@ -28,6 +28,10 @@ export default function bindActionCreators(actionCreators, dispatch) {
     return bindActionCreator(actionCreators, dispatch)
   }

+  if (Array.isArray(actionCreators)) {
+    return actionCreators.map(ac => bindActionCreator(ac, dispatch));
+  }
+
   if (typeof actionCreators !== 'object' || actionCreators === null) {
     throw new Error(
       `bindActionCreators expected an object or a function, instead received ${actionCreators === null ? 'null' : typeof actionCreators}. ` +
@aweary
Copy link
Contributor

aweary commented Jun 10, 2016

I think making bindActionCreator more polymorphic would be too much, how about just creating a wrapping function that does this for you?

function mapBindActionCreators(actions, dispatch) {
  return actions.map(action => bindActionCreator(action, dispatch)
}

@kennetpostigo
Copy link

If you have an array of them you could reduce() the array into an object and pass that to bindActionCreator

@gaearon
Copy link
Contributor

gaearon commented Jun 11, 2016

Yea, I think we’d like to avoid supporting every possible permutation here.

@gaearon gaearon closed this as completed Jun 11, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants