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.
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.
Most helpful comment
You can do this with ES7 object spread proposal:
or (vanilla ES6, needs
Object.assign
polyfill or Babel plugin)or (ES5, assumes
lodash
)