Skip to content

Commit eb6f8cf

Browse files
committed
fix possible falsy on reduxState
1 parent d248d0a commit eb6f8cf

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

src/components/connectAdvanced.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ export default function connectAdvanced(
226226
const [
227227
previousStateUpdateResult,
228228
forceComponentUpdateDispatch
229-
] = useState({ reduxStore: store.getState() })
229+
] = useState({ reduxState: store.getState() })
230230

231231
// Propagate any mapState/mapDispatch errors upwards
232232
if (previousStateUpdateResult && previousStateUpdateResult.error) {
@@ -239,7 +239,8 @@ export default function connectAdvanced(
239239
const childPropsFromStoreUpdate = useRef()
240240
const renderIsScheduled = useRef(false)
241241

242-
const latestReduxStore = useRef()
242+
const latestReduxState = useRef()
243+
const latestReduxStateIsValid = useRef(false)
243244

244245
const actualChildProps = usePureOnlyMemo(() => {
245246
// Tricky logic here:
@@ -255,17 +256,15 @@ export default function connectAdvanced(
255256
return childPropsFromStoreUpdate.current
256257
}
257258

258-
// TODO We're reading the store directly in render() here. Bad idea?
259-
// This will likely cause Bad Things (TM) to happen in Concurrent Mode.
260-
// Note that we do this because on renders _not_ caused by store updates, we need the latest store state
261-
// to determine what the child props should be.
262259
return childPropsSelector(
263-
latestReduxStore.current || previousStateUpdateResult.reduxStore,
260+
latestReduxStateIsValid.current
261+
? latestReduxState.current
262+
: previousStateUpdateResult.reduxState,
264263
wrapperProps
265264
)
266265
}, [
267-
latestReduxStore.current,
268-
previousStateUpdateResult.reduxStore,
266+
latestReduxStateIsValid.current,
267+
previousStateUpdateResult.reduxState,
269268
wrapperProps
270269
])
271270

@@ -323,7 +322,8 @@ export default function connectAdvanced(
323322

324323
// If the child props haven't changed, nothing to do here - cascade the subscription update
325324
if (newChildProps === lastChildProps.current) {
326-
latestReduxStore.current = latestStoreState
325+
latestReduxState.current = latestStoreState
326+
latestReduxStateIsValid.current = true
327327

328328
if (!renderIsScheduled.current) {
329329
notifyNestedSubs()
@@ -338,10 +338,11 @@ export default function connectAdvanced(
338338
renderIsScheduled.current = true
339339

340340
// If the child props _did_ change (or we caught an error), this wrapper component needs to re-render
341-
latestReduxStore.current = undefined
341+
latestReduxStateIsValid.current = false
342+
latestReduxState.current = undefined
342343
forceComponentUpdateDispatch({
343344
error,
344-
reduxStore: latestStoreState
345+
reduxState: latestStoreState
345346
})
346347
}
347348
}

src/hooks/useSelector.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ function useSelectorWithStoreAndSubscription(
1515
) {
1616
const [reduxState, forceRender] = useState(store.getState())
1717
const latestReduxState = useRef()
18+
const latestReduxStateIsValid = useRef(false)
1819

1920
const subscription = useMemo(() => new Subscription(store, contextSub), [
2021
store,
@@ -32,7 +33,9 @@ function useSelectorWithStoreAndSubscription(
3233
selector !== latestSelector.current ||
3334
latestSubscriptionCallbackError.current
3435
) {
35-
selectedState = selector(latestReduxState.current || reduxState)
36+
selectedState = selector(
37+
latestReduxStateIsValid.current ? latestReduxState.current : reduxState
38+
)
3639
} else {
3740
selectedState = latestSelectedState.current
3841
}
@@ -60,6 +63,7 @@ function useSelectorWithStoreAndSubscription(
6063

6164
if (equalityFn(newSelectedState, latestSelectedState.current)) {
6265
latestReduxState.current = newReduxState
66+
latestReduxStateIsValid.current = true
6367
return
6468
}
6569

@@ -71,6 +75,7 @@ function useSelectorWithStoreAndSubscription(
7175
// changed
7276
latestSubscriptionCallbackError.current = err
7377
}
78+
latestReduxStateIsValid.current = false
7479
latestReduxState.current = undefined
7580
forceRender(newReduxState)
7681
}

0 commit comments

Comments
 (0)