Skip to content

Reduce unnecessary calls to useSelector selector #1804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

sufian-slack
Copy link
Contributor

This change is intended to fix #1801 on the 8.x line

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.

Currently, useSelector(selector) will call the passed selector twice on
mount when it only needs to be called once on mount. This is
unnecessary, and in the case of expensive selectors, can be a
performance concern.
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.
@codesandbox-ci
Copy link

codesandbox-ci bot commented Aug 24, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit afa7c8e:

Sandbox Source
Vanilla Configuration
Vanilla Typescript Configuration
use-selector-twice Issue #1801

@netlify
Copy link

netlify bot commented Aug 24, 2021

✔️ Deploy Preview for react-redux-docs ready!

🔨 Explore the source changes: afa7c8e

🔍 Inspect the deploy log: https://app.netlify.com/sites/react-redux-docs/deploys/61266ee0617fc30007ec4234

😎 Browse the preview: https://deploy-preview-1804--react-redux-docs.netlify.app

@markerikson
Copy link
Contributor

Looks great! Let's do this.

(There's a very good chance this will get immediately replaced as I try migrating over to the new useSyncExternalStore API, but this is a good starting point, and the tests are still valuable)

@markerikson markerikson merged commit a3503c1 into reduxjs:master Sep 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

useSelector's provided selector function is called twice on mount
2 participants