@@ -21,58 +21,15 @@ function getDisplayName(WrappedComponent) {
21
21
// Helps track hot reloading.
22
22
let nextVersion = 0
23
23
24
- export default function connect ( mapStateToProps , mapDispatchToProps , mergeProps , options = { } ) {
25
- const shouldSubscribe = Boolean ( mapStateToProps )
26
- const finalMapStateToProps = mapStateToProps || defaultMapStateToProps
27
- const finalMapDispatchToProps = isPlainObject ( mapDispatchToProps ) ?
28
- wrapActionCreators ( mapDispatchToProps ) :
29
- mapDispatchToProps || defaultMapDispatchToProps
24
+ export function connectFactory ( mapStateToPropsFactory , mapDispatchToPropsFactory , mergeProps , options = { } ) {
30
25
const finalMergeProps = mergeProps || defaultMergeProps
31
- const shouldUpdateStateProps = finalMapStateToProps . length > 1
32
- const shouldUpdateDispatchProps = finalMapDispatchToProps . length > 1
26
+ const finalMapStateToPropsFactory = mapStateToPropsFactory || ( ) => defaultMapStateToProps
27
+ const finalMapDispatchToPropsFactory = mapDispatchToPropsFactory || ( ) => defaultMapDispatchToProps
33
28
const { pure = true , withRef = false } = options
34
29
35
30
// Helps track hot reloading.
36
31
const version = nextVersion ++
37
32
38
- function computeStateProps ( store , props ) {
39
- const state = store . getState ( )
40
- const stateProps = shouldUpdateStateProps ?
41
- finalMapStateToProps ( state , props ) :
42
- finalMapStateToProps ( state )
43
-
44
- invariant (
45
- isPlainObject ( stateProps ) ,
46
- '`mapStateToProps` must return an object. Instead received %s.' ,
47
- stateProps
48
- )
49
- return stateProps
50
- }
51
-
52
- function computeDispatchProps ( store , props ) {
53
- const { dispatch } = store
54
- const dispatchProps = shouldUpdateDispatchProps ?
55
- finalMapDispatchToProps ( dispatch , props ) :
56
- finalMapDispatchToProps ( dispatch )
57
-
58
- invariant (
59
- isPlainObject ( dispatchProps ) ,
60
- '`mapDispatchToProps` must return an object. Instead received %s.' ,
61
- dispatchProps
62
- )
63
- return dispatchProps
64
- }
65
-
66
- function computeNextState ( stateProps , dispatchProps , parentProps ) {
67
- const mergedProps = finalMergeProps ( stateProps , dispatchProps , parentProps )
68
- invariant (
69
- isPlainObject ( mergedProps ) ,
70
- '`mergeProps` must return an object. Instead received %s.' ,
71
- mergedProps
72
- )
73
- return mergedProps
74
- }
75
-
76
33
return function wrapWithConnect ( WrappedComponent ) {
77
34
class Connect extends Component {
78
35
shouldComponentUpdate ( nextProps , nextState ) {
@@ -88,11 +45,11 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
88
45
let mapStateProducedChange = false
89
46
let dispatchPropsChanged = false
90
47
91
- if ( storeChanged || ( propsChanged && shouldUpdateStateProps ) ) {
48
+ if ( storeChanged || ( propsChanged && this . shouldUpdateStateProps ) ) {
92
49
mapStateProducedChange = this . updateStateProps ( nextProps )
93
50
}
94
51
95
- if ( propsChanged && shouldUpdateDispatchProps ) {
52
+ if ( propsChanged && this . shouldUpdateDispatchProps ) {
96
53
dispatchPropsChanged = this . updateDispatchProps ( nextProps )
97
54
}
98
55
@@ -116,22 +73,61 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
116
73
`or explicitly pass "store" as a prop to "${ this . constructor . displayName } ".`
117
74
)
118
75
119
- this . stateProps = computeStateProps ( this . store , props )
120
- this . dispatchProps = computeDispatchProps ( this . store , props )
76
+ this . configure ( )
77
+ this . stateProps = this . computeStateProps ( )
78
+ this . dispatchProps = this . computeDispatchProps ( )
121
79
this . state = { storeState : null }
122
80
this . updateState ( )
123
81
}
124
82
125
- computeNextState ( props = this . props ) {
126
- return computeNextState (
127
- this . stateProps ,
128
- this . dispatchProps ,
129
- props
83
+ configure ( ) {
84
+ this . shouldSubscribe = Boolean ( mapStateToPropsFactory )
85
+ this . finalMapStateToProps = finalMapStateToPropsFactory ( ) ;
86
+ this . finalMapDispatchToProps = finalMapDispatchToPropsFactory ( ) ;
87
+ this . shouldUpdateStateProps = this . finalMapStateToProps . length > 1
88
+ this . shouldUpdateDispatchProps = this . finalMapDispatchToProps . length > 1
89
+ }
90
+
91
+ computeStateProps ( props = this . props ) {
92
+ const state = this . store . getState ( )
93
+ const stateProps = this . shouldUpdateStateProps ?
94
+ this . finalMapStateToProps ( state , props ) :
95
+ this . finalMapStateToProps ( state )
96
+
97
+ invariant (
98
+ isPlainObject ( stateProps ) ,
99
+ '`mapStateToProps` must return an object. Instead received %s.' ,
100
+ stateProps
101
+ )
102
+ return stateProps
103
+ }
104
+
105
+ computeDispatchProps ( props = this . props ) {
106
+ const { dispatch } = this . store
107
+ const dispatchProps = this . shouldUpdateDispatchProps ?
108
+ this . finalMapDispatchToProps ( dispatch , props ) :
109
+ this . finalMapDispatchToProps ( dispatch )
110
+
111
+ invariant (
112
+ isPlainObject ( dispatchProps ) ,
113
+ '`mapDispatchToProps` must return an object. Instead received %s.' ,
114
+ dispatchProps
130
115
)
116
+ return dispatchProps
117
+ }
118
+
119
+ computeNextState ( parentProps = this . props ) {
120
+ const mergedProps = finalMergeProps ( this . stateProps , this . dispatchProps , parentProps )
121
+ invariant (
122
+ isPlainObject ( mergedProps ) ,
123
+ '`mergeProps` must return an object. Instead received %s.' ,
124
+ mergedProps
125
+ )
126
+ return mergedProps
131
127
}
132
128
133
129
updateStateProps ( props = this . props ) {
134
- const nextStateProps = computeStateProps ( this . store , props )
130
+ const nextStateProps = this . computeStateProps ( props )
135
131
if ( shallowEqual ( nextStateProps , this . stateProps ) ) {
136
132
return false
137
133
}
@@ -141,7 +137,7 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
141
137
}
142
138
143
139
updateDispatchProps ( props = this . props ) {
144
- const nextDispatchProps = computeDispatchProps ( this . store , props )
140
+ const nextDispatchProps = this . computeDispatchProps ( props )
145
141
if ( shallowEqual ( nextDispatchProps , this . dispatchProps ) ) {
146
142
return false
147
143
}
@@ -159,7 +155,7 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
159
155
}
160
156
161
157
trySubscribe ( ) {
162
- if ( shouldSubscribe && ! this . unsubscribe ) {
158
+ if ( this . shouldSubscribe && ! this . unsubscribe ) {
163
159
this . unsubscribe = this . store . subscribe ( ::this . handleChange )
164
160
this . handleChange ( )
165
161
}
@@ -226,6 +222,7 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
226
222
this . version = version
227
223
228
224
// Update the state and bindings.
225
+ this . configure ( )
229
226
this . trySubscribe ( )
230
227
this . updateStateProps ( )
231
228
this . updateDispatchProps ( )
@@ -236,3 +233,12 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
236
233
return hoistStatics ( Connect , WrappedComponent )
237
234
}
238
235
}
236
+
237
+ export function connect ( mapStateToProps , mapDispatchToProps , mergeProps , options ) {
238
+ return connectFactory (
239
+ mapStateToProps ? ( ) => mapStateToProps : null ,
240
+ mapDispatchToProps ? ( ) => mapDispatchToProps : null ,
241
+ mergeProps ,
242
+ options
243
+ )
244
+ }
0 commit comments