@@ -3,7 +3,7 @@ import invariant from 'invariant'
3
3
import React , { Component , createElement } from 'react'
4
4
5
5
import { ReactReduxContext } from "./context" ;
6
- import { storeShape } from '../utils/PropTypes '
6
+ import shallowEqual from '../utils/shallowEqual '
7
7
8
8
let hotReloadingVersion = 0
9
9
const dummyState = { }
@@ -104,6 +104,67 @@ export default function connectAdvanced(
104
104
WrappedComponent
105
105
}
106
106
107
+
108
+ class ConnectInner extends Component {
109
+ constructor ( props ) {
110
+ super ( props ) ;
111
+
112
+ this . state = {
113
+ storeState : props . storeState ,
114
+ wrapperProps : props . wrapperProps ,
115
+ renderCount : 0 ,
116
+ store : props . store ,
117
+ error : null ,
118
+ childPropsSelector : this . createChildSelector ( props . store ) ,
119
+ childProps : { } ,
120
+ }
121
+
122
+ this . state = {
123
+ ...this . state ,
124
+ ...ConnectInner . getChildPropsState ( props , this . state )
125
+ }
126
+ }
127
+
128
+ createChildSelector ( store = this . state . store ) {
129
+ return selectorFactory ( store . dispatch , selectorFactoryOptions )
130
+ }
131
+
132
+ static getChildPropsState ( props , state ) {
133
+ try {
134
+ const nextProps = state . childPropsSelector ( state . storeState , props . wrapperProps )
135
+ if ( nextProps === state . childProps ) return null
136
+ return { childProps : nextProps }
137
+ } catch ( error ) {
138
+ return { error }
139
+ }
140
+ }
141
+
142
+ static getDerivedStateFromProps ( props , state ) {
143
+ if ( ( connectOptions . pure && shallowEqual ( props . wrapperProps , state . wrapperProps ) ) || state . error ) {
144
+ return null ;
145
+ }
146
+
147
+ const nextChildProps = ConnectInner . getChildPropsState ( props , state )
148
+
149
+ return {
150
+ ...nextChildProps ,
151
+ wrapperProps : props . wrapperProps ,
152
+ }
153
+ }
154
+
155
+ shouldComponentUpdate ( nextProps , nextState ) {
156
+ return nextState . childProps !== this . state . childProps ;
157
+ }
158
+
159
+ render ( ) {
160
+ if ( this . state . error ) {
161
+ throw this . state . error ;
162
+ }
163
+
164
+ return < WrappedComponent { ...this . state . childProps } />
165
+ }
166
+ }
167
+
107
168
class Connect extends Component {
108
169
constructor ( props ) {
109
170
super ( props )
@@ -113,8 +174,8 @@ export default function connectAdvanced(
113
174
this . storeState = null ;
114
175
115
176
116
- this . setWrappedInstance = this . setWrappedInstance . bind ( this )
117
- this . renderChild = this . renderChild . bind ( this ) ;
177
+ // this.setWrappedInstance = this.setWrappedInstance.bind(this)
178
+ this . renderInner = this . renderInner . bind ( this ) ;
118
179
119
180
// TODO How do we express the invariant of needing a Provider when it's used in render()?
120
181
/*
@@ -181,15 +242,16 @@ export default function connectAdvanced(
181
242
// instance. a singleton memoized selector would then be holding a reference to the
182
243
// instance, preventing the instance from being garbage collected, and that would be bad
183
244
const withExtras = { ...props }
184
- if ( withRef ) withExtras . ref = this . setWrappedInstance
245
+ // if (withRef) withExtras.ref = this.setWrappedInstance
185
246
if ( renderCountProp ) withExtras [ renderCountProp ] = this . renderCount ++
186
247
187
248
return withExtras
188
249
}
189
250
190
- renderChild ( providerValue ) {
191
- const { storeState, dispatch } = providerValue ;
251
+ renderInner ( providerValue ) {
252
+ const { storeState, store } = providerValue ;
192
253
254
+ /*
193
255
this.storeState = storeState;
194
256
195
257
if(this.selector) {
@@ -214,12 +276,14 @@ export default function connectAdvanced(
214
276
}
215
277
216
278
return this.renderedElement;
279
+ */
280
+ return < ConnectInner storeState = { storeState } store = { store } wrapperProps = { this . props } />
217
281
}
218
282
219
283
render ( ) {
220
284
return (
221
285
< ReactReduxContext . Consumer >
222
- { this . renderChild }
286
+ { this . renderInner }
223
287
</ ReactReduxContext . Consumer >
224
288
)
225
289
}
0 commit comments