Skip to content

Commit 647195d

Browse files
committed
save a ref when selector does not trigger an update
1 parent 3d52f3a commit 647195d

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/hooks/useSelector.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ function useSelectorWithStoreAndSubscription(
1414
contextSub
1515
) {
1616
const [reduxState, forceRender] = useState(store.getState())
17+
const latestReduxState = useRef()
1718

1819
const subscription = useMemo(() => new Subscription(store, contextSub), [
1920
store,
@@ -31,7 +32,7 @@ function useSelectorWithStoreAndSubscription(
3132
selector !== latestSelector.current ||
3233
latestSubscriptionCallbackError.current
3334
) {
34-
selectedState = selector(reduxState)
35+
selectedState = selector(latestReduxState.current || reduxState)
3536
} else {
3637
selectedState = latestSelectedState.current
3738
}
@@ -58,6 +59,7 @@ function useSelectorWithStoreAndSubscription(
5859
const newSelectedState = latestSelector.current(newReduxState)
5960

6061
if (equalityFn(newSelectedState, latestSelectedState.current)) {
62+
latestReduxState.current = newReduxState
6163
return
6264
}
6365

@@ -69,7 +71,7 @@ function useSelectorWithStoreAndSubscription(
6971
// changed
7072
latestSubscriptionCallbackError.current = err
7173
}
72-
74+
latestReduxState.current = undefined
7375
forceRender(newReduxState)
7476
}
7577

0 commit comments

Comments
 (0)