Przemek Rakowski
Przemek Rakowski

Reputation: 41

Why use bindActionCreator instead of passing an object

function mapStateToProps(state) {
 return { todos: state.todos }
}

function mapDispatchToProps(dispatch) {
 return { actions: bindActionCreators(actionCreators, dispatch) }
}

export default connect(mapStateToProps, mapDispatchToProps)(TodoApp)

why would i use mapDispatchToProps and bindActionCreators as second prop if

export default connect(mapStateToProps, { getSthAction })(TodoApp)

this way it works as well? is there any difference?

Upvotes: 1

Views: 200

Answers (1)

markerikson
markerikson

Reputation: 67539

The only difference in those examples is that your mapDispatch function will result in this.props.actions.someAction(), rather than this.props.someAction(), because you're explicitly returning the result of bindActionCreators as a field called actions.

I personally recommend always using the "object shorthand" version - to me, there's never a good reason to actually write a separate mapDispatch function.

Upvotes: 1

Related Questions