@@ -6,6 +6,7 @@ import wrapActionCreators from '../utils/wrapActionCreators'
6
6
import hoistStatics from 'hoist-non-react-statics'
7
7
import invariant from 'invariant'
8
8
9
+ const defaultMemoizer = ( fn ) => fn
9
10
const defaultMapStateToProps = ( ) => ( { } )
10
11
const defaultMapDispatchToProps = dispatch => ( { dispatch } )
11
12
const defaultMergeProps = ( stateProps , dispatchProps , parentProps ) => ( {
@@ -30,16 +31,16 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
30
31
const finalMergeProps = mergeProps || defaultMergeProps
31
32
const shouldUpdateStateProps = finalMapStateToProps . length > 1
32
33
const shouldUpdateDispatchProps = finalMapDispatchToProps . length > 1
33
- const { pure = true , withRef = false } = options
34
+ const { pure = true , withRef = false , memoize = defaultMemoizer } = options
34
35
35
36
// Helps track hot reloading.
36
37
const version = nextVersion ++
37
38
38
- function computeStateProps ( store , props ) {
39
+ function computeStateProps ( mapState , store , props ) {
39
40
const state = store . getState ( )
40
41
const stateProps = shouldUpdateStateProps ?
41
- finalMapStateToProps ( state , props ) :
42
- finalMapStateToProps ( state )
42
+ mapState ( state , props ) :
43
+ mapState ( state )
43
44
44
45
invariant (
45
46
isPlainObject ( stateProps ) ,
@@ -49,11 +50,11 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
49
50
return stateProps
50
51
}
51
52
52
- function computeDispatchProps ( store , props ) {
53
+ function computeDispatchProps ( mapDispatch , store , props ) {
53
54
const { dispatch } = store
54
55
const dispatchProps = shouldUpdateDispatchProps ?
55
- finalMapDispatchToProps ( dispatch , props ) :
56
- finalMapDispatchToProps ( dispatch )
56
+ mapDispatch ( dispatch , props ) :
57
+ mapDispatch ( dispatch )
57
58
58
59
invariant (
59
60
isPlainObject ( dispatchProps ) ,
@@ -115,9 +116,13 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
115
116
`Either wrap the root component in a <Provider>, ` +
116
117
`or explicitly pass "store" as a prop to "${ this . constructor . displayName } ".`
117
118
)
119
+ }
118
120
119
- this . stateProps = computeStateProps ( this . store , props )
120
- this . dispatchProps = computeDispatchProps ( this . store , props )
121
+ componentWillMount ( ) {
122
+ this . finalMapStateToProps = memoize ( finalMapStateToProps )
123
+ this . finalMapDispatchToProps = memoize ( finalMapDispatchToProps )
124
+ this . stateProps = computeStateProps ( this . finalMapStateToProps , this . store , this . props )
125
+ this . dispatchProps = computeDispatchProps ( this . finalMapDispatchToProps , this . store , this . props )
121
126
this . state = { storeState : null }
122
127
this . updateState ( )
123
128
}
@@ -131,7 +136,7 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
131
136
}
132
137
133
138
updateStateProps ( props = this . props ) {
134
- const nextStateProps = computeStateProps ( this . store , props )
139
+ const nextStateProps = computeStateProps ( this . finalMapStateToProps , this . store , props )
135
140
if ( shallowEqual ( nextStateProps , this . stateProps ) ) {
136
141
return false
137
142
}
@@ -141,7 +146,7 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
141
146
}
142
147
143
148
updateDispatchProps ( props = this . props ) {
144
- const nextDispatchProps = computeDispatchProps ( this . store , props )
149
+ const nextDispatchProps = computeDispatchProps ( this . finalMapDispatchToProps , this . store , props )
145
150
if ( shallowEqual ( nextDispatchProps , this . dispatchProps ) ) {
146
151
return false
147
152
}
@@ -226,6 +231,7 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
226
231
this . version = version
227
232
228
233
// Update the state and bindings.
234
+ this . componentWillMount ( )
229
235
this . trySubscribe ( )
230
236
this . updateStateProps ( )
231
237
this . updateDispatchProps ( )
0 commit comments