Skip to content
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

Avoid calling selector when the snapshot doesn't change #5084

Closed
wants to merge 1 commit into from

Conversation

Andarist
Copy link
Member

Copy link

changeset-bot bot commented Sep 18, 2024

⚠️ No Changeset found

Latest commit: 1967780

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@davidkpiano
Copy link
Member

I think there may be a hidden problem with this logic:

const Component = (props) => {
  const items = useSelector(store, s => {
    return s.context.items.map(item => ({
      ...item,
      isSelected: props.selected === item.id
    }));
  });
}

Not the best example but the point is that if we're selecting something that mixes in data outside the snapshot (whether that's best practice or not), we may end up with stale data.

@Andarist
Copy link
Member Author

That's a fair concern - this is how the React team has implemented' useSyncExternalStoreWithSelector', so I just used their logic here.

@TkDodo
Copy link
Contributor

TkDodo commented Sep 18, 2024

Not the best example but the point is that if we're selecting something that mixes in data outside the snapshot (whether that's best practice or not), we may end up with stale data.

Ah, I see. closing over values should still work - we could compare the selectorFn for equality but that wouldn't work with inline selectors :/

@Andarist
Copy link
Member Author

For context, this is where the original useSyncExternalStoreWithSelector is returning early:
https://github.com/facebook/react/blob/8dfbd16fce9077ab4e5fe85a7b86fa7c97a5ae04/packages/use-sync-external-store/src/useSyncExternalStoreWithSelector.js#L83-L86

@davidkpiano
Copy link
Member

Closing as we went with a different approach

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.

3 participants