Skip to content

Latest commit

 

History

History
51 lines (39 loc) · 1.72 KB

mapDispatchToProps-prefer-shorthand.md

File metadata and controls

51 lines (39 loc) · 1.72 KB

Enforces that mapDispatchToProps uses a shorthand method to wrap actions in dispatch calls whenever possible. (react-redux/mapDispatchToProps-prefer-shorthand)

...connect supports an “object shorthand” form for the mapDispatchToProps argument: if you pass an object full of action creators instead of a function, connect will automatically call bindActionCreators for you internally. We recommend always using the “object shorthand” form of mapDispatchToProps, unless you have a specific reason to customize the dispatching behavior.

source

Rule details

The following patterns are considered incorrect:

const mapDispatchToProps = (dispatch) => ({
  action: () => dispatch(action())
})
export default connect(null, mapDispatchToProps)(Component)
const mapDispatchToProps = (dispatch) => ({
  action: () => dispatch(action()),
  action1: (arg1, arg2) => dispatch(action(arg1, arg2))
})
export default connect(null, mapDispatchToProps)(Component)

The following patterns are considered correct:

export default connect(null, { action })(Component)
const mapDispatchToProps = { action }
export default connect(null, mapDispatchToProps)(Component)
const mapDispatchToProps = (dispatch) => ({
  action: () => dispatch(actionHelper(true))
})
export default connect(null, mapDispatchToProps)(Component)
const mapDispatchToProps = (dispatch) => ({
  action: () => dispatch(action()),
  action1: (arg1, arg2) => dispatch(action(arg1 + arg2))
})
export default connect(null, mapDispatchToProps)(Component)