@@ -12,6 +12,10 @@ import Subscription from '../utils/Subscription'
12
12
13
13
import { ReactReduxContext } from './Context'
14
14
15
+ const EMPTY_ARRAY = [ ]
16
+
17
+ const NO_SUBSCRIPTION_ARRAY = [ null , null ]
18
+
15
19
const stringifyComponent = Comp => {
16
20
try {
17
21
return JSON . stringify ( Comp )
@@ -20,6 +24,13 @@ const stringifyComponent = Comp => {
20
24
}
21
25
}
22
26
27
+ function storeStateUpdatesReducer ( state , action ) {
28
+ const [ , updateCount ] = state
29
+ return [ action . payload , updateCount + 1 ]
30
+ }
31
+
32
+ const initStateUpdates = ( ) => [ null , 0 ]
33
+
23
34
export default function connectAdvanced (
24
35
/*
25
36
selectorFactory is a func that is responsible for returning the selector function used to
@@ -132,11 +143,6 @@ export default function connectAdvanced(
132
143
133
144
const usePureOnlyMemo = pure ? useMemo : x => x ( )
134
145
135
- function storeStateUpdatesReducer ( state , action ) {
136
- const [ , updateCount = 0 ] = state
137
- return [ action . payload , updateCount + 1 ]
138
- }
139
-
140
146
function ConnectFunction ( props ) {
141
147
const [ propsContext , forwardedRef , wrapperProps ] = useMemo ( ( ) => {
142
148
const { context, forwardedRef, ...wrapperProps } = props
@@ -168,7 +174,7 @@ export default function connectAdvanced(
168
174
} , [ store ] )
169
175
170
176
const [ subscription , notifyNestedSubs ] = useMemo ( ( ) => {
171
- if ( ! shouldHandleStateChanges ) return [ ]
177
+ if ( ! shouldHandleStateChanges ) return NO_SUBSCRIPTION_ARRAY
172
178
173
179
// parentSub's source should match where store came from: props vs. context. A component
174
180
// connected to the store via props shouldn't use subscription from context, or vice versa.
@@ -197,7 +203,8 @@ export default function connectAdvanced(
197
203
198
204
const [ [ previousStateUpdateResult ] , dispatch ] = useReducer (
199
205
storeStateUpdatesReducer ,
200
- [ ]
206
+ EMPTY_ARRAY ,
207
+ initStateUpdates
201
208
)
202
209
203
210
if ( previousStateUpdateResult && previousStateUpdateResult . error ) {
@@ -311,6 +318,7 @@ export default function connectAdvanced(
311
318
}
312
319
313
320
const Connect = pure ? React . memo ( ConnectFunction ) : ConnectFunction
321
+
314
322
Connect . WrappedComponent = WrappedComponent
315
323
Connect . displayName = displayName
316
324
0 commit comments