From e57b51ad7017e99162fe1b171e9972a5ac0e2321 Mon Sep 17 00:00:00 2001 From: seanstrom Date: Wed, 20 Apr 2016 22:32:17 -0700 Subject: [PATCH 1/2] refactor assigning mapDispatch --- src/components/connect.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/components/connect.js b/src/components/connect.js index 3b60ebbce..f05ca988c 100644 --- a/src/components/connect.js +++ b/src/components/connect.js @@ -3,6 +3,7 @@ import storeShape from '../utils/storeShape' import shallowEqual from '../utils/shallowEqual' import wrapActionCreators from '../utils/wrapActionCreators' import warning from '../utils/warning' +import isFunction from 'lodash/isFunction' import isPlainObject from 'lodash/isPlainObject' import hoistStatics from 'hoist-non-react-statics' import invariant from 'invariant' @@ -19,6 +20,14 @@ function getDisplayName(WrappedComponent) { return WrappedComponent.displayName || WrappedComponent.name || 'Component' } +function getMapDispatch(value) { + switch (true) { + case isFunction(value): return value + case isPlainObject(value): return wrapActionCreators(value) + default: return defaultMapDispatchToProps + } +} + let errorObject = { value: null } function tryCatch(fn, ctx) { try { @@ -35,15 +44,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 From 50b474b4d013b72cd550aef0d68a63523fe9296e Mon Sep 17 00:00:00 2001 From: seanstrom Date: Thu, 21 Apr 2016 10:17:05 -0700 Subject: [PATCH 2/2] use typeof instead of isFunction --- src/components/connect.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/components/connect.js b/src/components/connect.js index f05ca988c..cb70352fe 100644 --- a/src/components/connect.js +++ b/src/components/connect.js @@ -3,7 +3,6 @@ import storeShape from '../utils/storeShape' import shallowEqual from '../utils/shallowEqual' import wrapActionCreators from '../utils/wrapActionCreators' import warning from '../utils/warning' -import isFunction from 'lodash/isFunction' import isPlainObject from 'lodash/isPlainObject' import hoistStatics from 'hoist-non-react-statics' import invariant from 'invariant' @@ -22,7 +21,7 @@ function getDisplayName(WrappedComponent) { function getMapDispatch(value) { switch (true) { - case isFunction(value): return value + case (typeof value === 'function'): return value case isPlainObject(value): return wrapActionCreators(value) default: return defaultMapDispatchToProps }