Redux: Could bindActionCreators optionally take a list of action objects?

Created on 30 Jul 2015  ·  9Comments  ·  Source: reduxjs/redux

We've got a few places in our code where we're passing multiple objects of bound actions into components. It would great to have the optionally pass a list of action objects into bindActionCreators.

bindActionCreators([SomeActions, OtherActions], dispatch)

The caveat of course would be duplicate actions would get overwritten by the last one processed.

Most helpful comment

You can do this with ES7 object spread proposal:

bindActionCreators({ ...SomeActions, ...OtherActions }, dispatch)

or (vanilla ES6, needs Object.assign polyfill or Babel plugin)

bindActionCreators(Object.assign({}, SomeActions, OtherActions), dispatch)

or (ES5, assumes lodash)

var assign = require('lodash/object/assign');

bindActionCreators(assign({}, SomeActions, OtherActions), dispatch)

All 9 comments

You can do this with ES7 object spread proposal:

bindActionCreators({ ...SomeActions, ...OtherActions }, dispatch)

or (vanilla ES6, needs Object.assign polyfill or Babel plugin)

bindActionCreators(Object.assign({}, SomeActions, OtherActions), dispatch)

or (ES5, assumes lodash)

var assign = require('lodash/object/assign');

bindActionCreators(assign({}, SomeActions, OtherActions), dispatch)

Ah excellent, that's even easier. Thanks!

🙌

This is helpful! Thanks!

Super helpful! :star2:

@gaearon should this possibly be added to the tips section in the bindActionCreators API doc?

A pull request is welcome :wink:
I’m not actively working on the docs right now so my answer is the same: “if you’d like something to be in the docs, please send a PR”.

Not sure if this will help anyone, but I was trying to combine my app actions and Actions from react-native-router-flux, and below worked for me.

import * as AppActions from './actions';
import { Actions } from 'react-native-router-flux';

function mapDispatchToProps(dispatch) {
  return { actions: {...bindActionCreators(AppActions , dispatch), ...Actions } };
}

Not sure if this will help anyone, but I was trying to combine my app actions and Actions from react-native-router-flux, and below worked for me.

This is a little confusing because Actions from react-native-router-flux seem to have nothing in common with Redux, and probably shouldn’t be in mapDispatchToProps at all. Just use them directly in your components.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jbri7357 picture jbri7357  ·  3Comments

ramakay picture ramakay  ·  3Comments

parallelthought picture parallelthought  ·  3Comments

ms88privat picture ms88privat  ·  3Comments

mickeyreiss-visor picture mickeyreiss-visor  ·  3Comments