@@ -5,33 +5,46 @@ import isPlainObject from '../utils/isPlainObject';
55import wrapActionCreators from '../utils/wrapActionCreators' ;
66import invariant from 'invariant' ;
77
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
1414} ) ;
1515
1616function getDisplayName ( Component ) {
1717 return Component . displayName || Component . name || 'Component' ;
1818}
1919
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+
2033export default function createConnect ( React ) {
2134 const { Component, PropTypes } = React ;
2235 const storeShape = createStoreShape ( PropTypes ) ;
2336
2437 return function connect (
25- mapState = defaultMapState ,
26- mapDispatchOrActionCreators = defaultMapDispatch ,
38+ mapStateToProps = defaultMapStateToProps ,
39+ actionCreatorsOrMapDispatchToProps = defaultMapDispatchToProps ,
2740 mergeProps = defaultMergeProps
2841 ) {
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 ;
3346
34- return DecoratedComponent => class ConnectDecorator extends Component {
47+ return DecoratedComponent => class Connect extends Component {
3548 static displayName = `Connect(${ getDisplayName ( DecoratedComponent ) } )` ;
3649 static DecoratedComponent = DecoratedComponent ;
3750
@@ -40,21 +53,10 @@ export default function createConnect(React) {
4053 } ;
4154
4255 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 ) ;
5860 }
5961
6062 constructor ( props , context ) {
@@ -81,40 +83,40 @@ export default function createConnect(React) {
8183
8284 handleChange ( props = this . props ) {
8385 const nextState = this . mapState ( props , this . context ) ;
84- if ( ! this . isSliceEqual ( this . state . slice , nextState . slice ) ) {
86+ if ( ! areStatePropsEqual ( this . state . stateProps , nextState . stateProps ) ) {
8587 this . setState ( nextState ) ;
8688 }
8789 }
8890
8991 mapState ( props = this . props , context = this . context ) {
9092 const state = context . store . getState ( ) ;
91- const slice = mapState ( state ) ;
93+ const stateProps = mapStateToProps ( state ) ;
9294
9395 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
9799 ) ;
98100
99- return { slice } ;
101+ return { stateProps } ;
100102 }
101103
102104 mapDispatch ( context = this . context ) {
103105 const { dispatch } = context . store ;
104- const actionCreators = mapDispatch ( dispatch ) ;
106+ const dispatchProps = mapDispatchToProps ( dispatch ) ;
105107
106108 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
110112 ) ;
111113
112- return { actionCreators } ;
114+ return { dispatchProps } ;
113115 }
114116
115117 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 ) ;
118120
119121 invariant (
120122 isPlainObject ( merged ) ,
0 commit comments