@@ -34,8 +34,8 @@ export default function createConnect(React) {
34
34
// Helps track hot reloading.
35
35
const version = nextVersion ++ ;
36
36
37
- function computeStateProps ( context ) {
38
- const state = context . store . getState ( ) ;
37
+ function computeStateProps ( store ) {
38
+ const state = store . getState ( ) ;
39
39
const stateProps = finalMapStateToProps ( state ) ;
40
40
invariant (
41
41
isPlainObject ( stateProps ) ,
@@ -45,8 +45,8 @@ export default function createConnect(React) {
45
45
return stateProps ;
46
46
}
47
47
48
- function computeDispatchProps ( context ) {
49
- const { dispatch } = context . store ;
48
+ function computeDispatchProps ( store ) {
49
+ const { dispatch } = store ;
50
50
const dispatchProps = finalMapDispatchToProps ( dispatch ) ;
51
51
invariant (
52
52
isPlainObject ( dispatchProps ) ,
@@ -72,7 +72,11 @@ export default function createConnect(React) {
72
72
static WrappedComponent = WrappedComponent ;
73
73
74
74
static contextTypes = {
75
- store : storeShape . isRequired
75
+ store : storeShape
76
+ } ;
77
+
78
+ static propTypes = {
79
+ store : storeShape
76
80
} ;
77
81
78
82
shouldComponentUpdate ( nextProps , nextState ) {
@@ -82,13 +86,22 @@ export default function createConnect(React) {
82
86
constructor ( props , context ) {
83
87
super ( props , context ) ;
84
88
this . version = version ;
85
- this . stateProps = computeStateProps ( context ) ;
86
- this . dispatchProps = computeDispatchProps ( context ) ;
89
+ this . store = props . store || context . store ;
90
+
91
+ invariant ( this . store ,
92
+ `Could not find "store" in either the context or ` +
93
+ `props of "${ this . constructor . displayName } ". ` +
94
+ `Either wrap the root component in a <Provider>, ` +
95
+ `or explicitly pass "store" as a prop to "${ this . constructor . displayName } ".`
96
+ ) ;
97
+
98
+ this . stateProps = computeStateProps ( this . store ) ;
99
+ this . dispatchProps = computeDispatchProps ( this . store ) ;
87
100
this . state = this . computeNextState ( ) ;
88
101
}
89
102
90
103
recomputeStateProps ( ) {
91
- const nextStateProps = computeStateProps ( this . context ) ;
104
+ const nextStateProps = computeStateProps ( this . store ) ;
92
105
if ( shallowEqual ( nextStateProps , this . stateProps ) ) {
93
106
return false ;
94
107
}
@@ -98,7 +111,7 @@ export default function createConnect(React) {
98
111
}
99
112
100
113
recomputeDispatchProps ( ) {
101
- const nextDispatchProps = computeDispatchProps ( this . context ) ;
114
+ const nextDispatchProps = computeDispatchProps ( this . store ) ;
102
115
if ( shallowEqual ( nextDispatchProps , this . dispatchProps ) ) {
103
116
return false ;
104
117
}
@@ -128,7 +141,7 @@ export default function createConnect(React) {
128
141
129
142
trySubscribe ( ) {
130
143
if ( shouldSubscribe && ! this . unsubscribe ) {
131
- this . unsubscribe = this . context . store . subscribe ( ::this . handleChange ) ;
144
+ this . unsubscribe = this . store . subscribe ( ::this . handleChange ) ;
132
145
this . handleChange ( ) ;
133
146
}
134
147
}
0 commit comments