Skip to content

Commit 72aa365

Browse files
committed
refactor connect#checkStateShape and calculate displayName right away
1 parent 9d16da2 commit 72aa365

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

src/components/connect.js

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ function getDisplayName(WrappedComponent) {
1818
return WrappedComponent.displayName || WrappedComponent.name || 'Component'
1919
}
2020

21-
function checkStateShape(componentDisplayName, stateProps, dispatch) {
21+
function checkStateShape(componentDisplayName, props, methodName) {
2222
invariant(
23-
isPlainObject(stateProps),
24-
'`%s %sToProps` must return an object. Instead received %s.',
23+
isPlainObject(props),
24+
'`%s %s` must return an object. Instead received %s.',
2525
componentDisplayName,
26-
dispatch ? 'mapDispatch' : 'mapState',
27-
stateProps
26+
methodName,
27+
props
2828
)
29-
return stateProps
29+
return props
3030
}
3131

3232
// Helps track hot reloading.
@@ -48,16 +48,12 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
4848

4949
function computeMergedProps(componentDisplayName, stateProps, dispatchProps, parentProps) {
5050
const mergedProps = finalMergeProps(stateProps, dispatchProps, parentProps)
51-
invariant(
52-
isPlainObject(mergedProps),
53-
'`%s mergeProps` must return an object. Instead received %s.',
54-
componentDisplayName,
55-
mergedProps
56-
)
57-
return mergedProps
51+
return checkStateShape(componentDisplayName, mergedProps, 'mergeProps')
5852
}
5953

6054
return function wrapWithConnect(WrappedComponent) {
55+
const connectDisplayName = `Connect(${getDisplayName(WrappedComponent)})`
56+
6157
class Connect extends Component {
6258
shouldComponentUpdate() {
6359
return !pure || this.haveOwnPropsChanged || this.hasStoreStateChanged
@@ -70,9 +66,9 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
7066

7167
invariant(this.store,
7268
`Could not find "store" in either the context or ` +
73-
`props of "${this.constructor.displayName}". ` +
69+
`props of "${connectDisplayName}". ` +
7470
`Either wrap the root component in a <Provider>, ` +
75-
`or explicitly pass "store" as a prop to "${this.constructor.displayName}".`
71+
`or explicitly pass "store" as a prop to "${connectDisplayName}".`
7672
)
7773

7874
const storeState = this.store.getState()
@@ -90,7 +86,7 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
9086
this.finalMapStateToProps(state, props) :
9187
this.finalMapStateToProps(state)
9288

93-
return checkStateShape(this.constructor.displayName, stateProps)
89+
return checkStateShape(connectDisplayName, stateProps, 'mapStateToProps')
9490
}
9591

9692
configureFinalMapState(store, props) {
@@ -102,7 +98,7 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
10298

10399
return isFactory ?
104100
this.computeStateProps(store, props) :
105-
checkStateShape(this.constructor.displayName, mappedState)
101+
checkStateShape(connectDisplayName, mappedState, 'mapStateToProps')
106102
}
107103

108104
computeDispatchProps(store, props) {
@@ -115,7 +111,7 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
115111
this.finalMapDispatchToProps(dispatch, props) :
116112
this.finalMapDispatchToProps(dispatch)
117113

118-
return checkStateShape(this.constructor.displayName, dispatchProps, true)
114+
return checkStateShape(connectDisplayName, dispatchProps, 'mapDispatchToProps')
119115
}
120116

121117
configureFinalMapDispatch(store, props) {
@@ -127,7 +123,7 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
127123

128124
return isFactory ?
129125
this.computeDispatchProps(store, props) :
130-
checkStateShape(this.constructor.displayName, mappedDispatch, true)
126+
checkStateShape(connectDisplayName, mappedDispatch, 'mapDispatchToProps')
131127
}
132128

133129
updateStatePropsIfNeeded() {
@@ -151,7 +147,7 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
151147
}
152148

153149
updateMergedPropsIfNeeded() {
154-
const nextMergedProps = computeMergedProps(this.constructor.displayName, this.stateProps, this.dispatchProps, this.props)
150+
const nextMergedProps = computeMergedProps(connectDisplayName, this.stateProps, this.dispatchProps, this.props)
155151
if (this.mergedProps && checkMergedEquals && shallowEqual(nextMergedProps, this.mergedProps)) {
156152
return false
157153
}
@@ -286,7 +282,7 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
286282
}
287283
}
288284

289-
Connect.displayName = `Connect(${getDisplayName(WrappedComponent)})`
285+
Connect.displayName = connectDisplayName
290286
Connect.WrappedComponent = WrappedComponent
291287
Connect.contextTypes = {
292288
store: storeShape

0 commit comments

Comments
 (0)