@@ -5,33 +5,46 @@ import isPlainObject from '../utils/isPlainObject';
5
5
import wrapActionCreators from '../utils/wrapActionCreators' ;
6
6
import invariant from 'invariant' ;
7
7
8
- const defaultMapState = ( ) => ( { } ) ;
9
- const defaultMapDispatch = dispatch => ( { dispatch } ) ;
10
- const defaultMergeProps = ( stateSlice , actionsCreators , props ) => ( {
11
- ...props ,
12
- ...stateSlice ,
13
- ...actionsCreators
8
+ const defaultMapStateToProps = ( ) => ( { } ) ;
9
+ const defaultMapDispatchToProps = dispatch => ( { dispatch } ) ;
10
+ const defaultMergeProps = ( stateProps , dispatchProps , parentProps ) => ( {
11
+ ...parentProps ,
12
+ ...stateProps ,
13
+ ...dispatchProps
14
14
} ) ;
15
15
16
16
function getDisplayName ( Component ) {
17
17
return Component . displayName || Component . name || 'Component' ;
18
18
}
19
19
20
+ function areStatePropsEqual ( stateProps , nextStateProps ) {
21
+ const isRefEqual = stateProps === nextStateProps ;
22
+ if (
23
+ isRefEqual ||
24
+ typeof stateProps !== 'object' ||
25
+ typeof nextStateProps !== 'object'
26
+ ) {
27
+ return isRefEqual ;
28
+ }
29
+
30
+ return shallowEqual ( stateProps , nextStateProps ) ;
31
+ }
32
+
20
33
export default function createConnect ( React ) {
21
34
const { Component, PropTypes } = React ;
22
35
const storeShape = createStoreShape ( PropTypes ) ;
23
36
24
37
return function connect (
25
- mapState = defaultMapState ,
26
- mapDispatchOrActionCreators = defaultMapDispatch ,
38
+ mapStateToProps = defaultMapStateToProps ,
39
+ actionCreatorsOrMapDispatchToProps = defaultMapDispatchToProps ,
27
40
mergeProps = defaultMergeProps
28
41
) {
29
- const shouldSubscribe = mapState !== defaultMapState ;
30
- const mapDispatch = isPlainObject ( mapDispatchOrActionCreators ) ?
31
- wrapActionCreators ( mapDispatchOrActionCreators ) :
32
- mapDispatchOrActionCreators ;
42
+ const shouldSubscribe = mapStateToProps !== defaultMapStateToProps ;
43
+ const mapDispatchToProps = isPlainObject ( actionCreatorsOrMapDispatchToProps ) ?
44
+ wrapActionCreators ( actionCreatorsOrMapDispatchToProps ) :
45
+ actionCreatorsOrMapDispatchToProps ;
33
46
34
- return DecoratedComponent => class ConnectDecorator extends Component {
47
+ return DecoratedComponent => class Connect extends Component {
35
48
static displayName = `Connect(${ getDisplayName ( DecoratedComponent ) } )` ;
36
49
static DecoratedComponent = DecoratedComponent ;
37
50
@@ -40,21 +53,10 @@ export default function createConnect(React) {
40
53
} ;
41
54
42
55
shouldComponentUpdate ( nextProps , nextState ) {
43
- return ( this . subscribed && ! this . isSliceEqual ( this . state . slice , nextState . slice ) ) ||
44
- ! shallowEqualScalar ( this . props , nextProps ) ;
45
- }
46
-
47
- isSliceEqual ( slice , nextSlice ) {
48
- const isRefEqual = slice === nextSlice ;
49
- if (
50
- isRefEqual ||
51
- typeof slice !== 'object' ||
52
- typeof nextSlice !== 'object'
53
- ) {
54
- return isRefEqual ;
55
- }
56
-
57
- return shallowEqual ( slice , nextSlice ) ;
56
+ return (
57
+ this . subscribed &&
58
+ ! areStatePropsEqual ( this . state . stateProps , nextState . stateProps )
59
+ ) || ! shallowEqualScalar ( this . props , nextProps ) ;
58
60
}
59
61
60
62
constructor ( props , context ) {
@@ -81,40 +83,40 @@ export default function createConnect(React) {
81
83
82
84
handleChange ( props = this . props ) {
83
85
const nextState = this . mapState ( props , this . context ) ;
84
- if ( ! this . isSliceEqual ( this . state . slice , nextState . slice ) ) {
86
+ if ( ! areStatePropsEqual ( this . state . stateProps , nextState . stateProps ) ) {
85
87
this . setState ( nextState ) ;
86
88
}
87
89
}
88
90
89
91
mapState ( props = this . props , context = this . context ) {
90
92
const state = context . store . getState ( ) ;
91
- const slice = mapState ( state ) ;
93
+ const stateProps = mapStateToProps ( state ) ;
92
94
93
95
invariant (
94
- isPlainObject ( slice ) ,
95
- '`mapState ` must return an object. Instead received %s.' ,
96
- slice
96
+ isPlainObject ( stateProps ) ,
97
+ '`mapStateToProps ` must return an object. Instead received %s.' ,
98
+ stateProps
97
99
) ;
98
100
99
- return { slice } ;
101
+ return { stateProps } ;
100
102
}
101
103
102
104
mapDispatch ( context = this . context ) {
103
105
const { dispatch } = context . store ;
104
- const actionCreators = mapDispatch ( dispatch ) ;
106
+ const dispatchProps = mapDispatchToProps ( dispatch ) ;
105
107
106
108
invariant (
107
- isPlainObject ( actionCreators ) ,
108
- '`mapDispatch ` must return an object. Instead received %s.' ,
109
- actionCreators
109
+ isPlainObject ( dispatchProps ) ,
110
+ '`mapDispatchToProps ` must return an object. Instead received %s.' ,
111
+ dispatchProps
110
112
) ;
111
113
112
- return { actionCreators } ;
114
+ return { dispatchProps } ;
113
115
}
114
116
115
117
merge ( props = this . props , state = this . state ) {
116
- const { slice , actionCreators } = state ;
117
- const merged = mergeProps ( slice , actionCreators , props ) ;
118
+ const { stateProps , dispatchProps } = state ;
119
+ const merged = mergeProps ( stateProps , dispatchProps , props ) ;
118
120
119
121
invariant (
120
122
isPlainObject ( merged ) ,
0 commit comments