Skip to content

Commit 75b2fba

Browse files
committed
Reduce the number of calls to the provided selector by one
By checking if the store state has differed prior to recalculating a selector, we can avoid unnecessary selector recalculation in most cases for components on mount.
1 parent 7f2ca90 commit 75b2fba

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

src/hooks/useSelector.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ function useSelectorWithStoreAndSubscription(
6565
function checkForUpdates() {
6666
try {
6767
const newStoreState = store.getState()
68+
// Avoid calling selector multiple times if the store's state has not changed
69+
if (newStoreState === latestStoreState.current) {
70+
return
71+
}
72+
6873
const newSelectedState = latestSelector.current(newStoreState)
6974

7075
if (equalityFn(newSelectedState, latestSelectedState.current)) {

0 commit comments

Comments
 (0)