Skip to content

Commit

Permalink
Reduce the number of calls to the provided selector by one
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sufian-slack committed Aug 24, 2021
1 parent 7f2ca90 commit 75b2fba
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/hooks/useSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ function useSelectorWithStoreAndSubscription(
function checkForUpdates() {
try {
const newStoreState = store.getState()
// Avoid calling selector multiple times if the store's state has not changed
if (newStoreState === latestStoreState.current) {
return
}

const newSelectedState = latestSelector.current(newStoreState)

if (equalityFn(newSelectedState, latestSelectedState.current)) {
Expand Down

0 comments on commit 75b2fba

Please sign in to comment.