Skip to content

Refactor assigment of mapDispatch in connect.js #363

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

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/components/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ function getDisplayName(WrappedComponent) {
return WrappedComponent.displayName || WrappedComponent.name || 'Component'
}

function getMapDispatch(value) {
switch (true) {
case (typeof value === 'function'): return value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a very surprising use of switch statement. At this point I think the original version was clearer.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that a switch statement here can be unusual. But we can translate that directly to guard statements:

function getMapDispatch(value) {
  if (typeof value === 'function') {
    return value
  }

  if (isPlainObject(value)) {
    return wrapActionCreators(value)
  }

  return defaultMapDispatchToProps
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or we can use if/else-if/else as well

function getMapDispatch(value) {
  if (typeof value === 'function') {
    return value
  } else if (isPlainObject(value)) {
    return wrapActionCreators(value)
  } else {
    return defaultMapDispatchToProps
  }
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main things I would promote are the function extraction, it adds some more clarity imo, and the revision to using isPlainObject as the second guard statement or else-if.

case isPlainObject(value): return wrapActionCreators(value)
default: return defaultMapDispatchToProps
}
}

let errorObject = { value: null }
function tryCatch(fn, ctx) {
try {
Expand All @@ -35,15 +43,7 @@ let nextVersion = 0
export default function connect(mapStateToProps, mapDispatchToProps, mergeProps, options = {}) {
const shouldSubscribe = Boolean(mapStateToProps)
const mapState = mapStateToProps || defaultMapStateToProps

let mapDispatch
if (typeof mapDispatchToProps === 'function') {
mapDispatch = mapDispatchToProps
} else if (!mapDispatchToProps) {
mapDispatch = defaultMapDispatchToProps
} else {
mapDispatch = wrapActionCreators(mapDispatchToProps)
}
const mapDispatch = getMapDispatch(mapDispatchToProps)

const finalMergeProps = mergeProps || defaultMergeProps
const { pure = true, withRef = false } = options
Expand Down