Skip to content

Commit 9a28e78

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 9ecceb2 commit 9a28e78

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

src/hooks/useSelector.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ function useSelectorWithStoreAndSubscription<TStoreState, TSelectedState>(
7171
function checkForUpdates() {
7272
try {
7373
const newStoreState = store.getState()
74+
// Avoid calling selector multiple times if the store's state has not changed
75+
if (newStoreState === latestStoreState.current) {
76+
return
77+
}
78+
7479
const newSelectedState = latestSelector.current!(newStoreState)
7580

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

0 commit comments

Comments
 (0)