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

[useSES/extra] Reuse old selection if possible #22307

Merged
merged 1 commit into from
Sep 13, 2021

Conversation

acdlite
Copy link
Collaborator

@acdlite acdlite commented Sep 13, 2021

When you pass an unmemoized selector to useSyncExternalStoreExtra, we have to reevaluate it on every render because we don't know whether it depends on new props.

However, after reevalutating it, we should still compare the result to the previous selection with isEqual and reuse the old one if it hasn't changed.

Originally I did not implement this, because if the selector changes due to new props or state, the component is going to have to re-render anyway. However, it's still useful to return a memoized selection when possible, because it may be the input to a downstream memoization.

In the test I wrote, the example I chose is selecting a list of items from the store, and passing the list as a prop to a memoized component. If the list prop is memoized, we can bail out.

When you pass an unmemoized selector to useSyncExternalStoreExtra, we
have to reevaluate it on every render because we don't know whether
it depends on new props.

However, after reevalutating it, we should still compare the result
to the previous selection with `isEqual` and reuse the old one if it
hasn't changed.

Originally I did not implement this, because if the selector changes due
to new props or state, the component is going to have to re-render
anyway. However, it's still useful to return a memoized selection
when possible, because it may be the input to a downstream memoization.

In the test I wrote, the example I chose is selecting a list of
items from the store, and passing the list as a prop to a memoized
component. If the list prop is memoized, we can bail out.
@facebook-github-bot facebook-github-bot added CLA Signed React Core Team Opened by a member of the React Core Team labels Sep 13, 2021
@sizebot
Copy link

sizebot commented Sep 13, 2021

Comparing: 33226fa...c99f5ea

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.min.js = 129.30 kB 129.30 kB = 41.22 kB 41.22 kB
oss-experimental/react-dom/cjs/react-dom.production.min.js = 132.12 kB 132.12 kB = 42.16 kB 42.16 kB
facebook-www/ReactDOM-prod.classic.js = 410.35 kB 410.35 kB = 75.95 kB 75.95 kB
facebook-www/ReactDOM-prod.modern.js = 398.91 kB 398.91 kB = 74.24 kB 74.24 kB
facebook-www/ReactDOMForked-prod.classic.js = 410.35 kB 410.35 kB = 75.95 kB 75.95 kB
oss-experimental/use-sync-external-store/cjs/use-sync-external-store-extra.development.js +30.84% 3.07 kB 4.02 kB +21.15% 1.27 kB 1.54 kB
oss-stable-semver/use-sync-external-store/cjs/use-sync-external-store-extra.development.js +30.84% 3.07 kB 4.02 kB +21.15% 1.27 kB 1.54 kB
oss-stable/use-sync-external-store/cjs/use-sync-external-store-extra.development.js +30.84% 3.07 kB 4.02 kB +21.15% 1.27 kB 1.54 kB
oss-experimental/use-sync-external-store/cjs/use-sync-external-store-extra.production.min.js +27.91% 0.76 kB 0.97 kB +19.06% 0.47 kB 0.56 kB
oss-stable-semver/use-sync-external-store/cjs/use-sync-external-store-extra.production.min.js +27.91% 0.76 kB 0.97 kB +19.06% 0.47 kB 0.56 kB
oss-stable/use-sync-external-store/cjs/use-sync-external-store-extra.production.min.js +27.91% 0.76 kB 0.97 kB +19.06% 0.47 kB 0.56 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
oss-experimental/use-sync-external-store/cjs/use-sync-external-store-extra.development.js +30.84% 3.07 kB 4.02 kB +21.15% 1.27 kB 1.54 kB
oss-stable-semver/use-sync-external-store/cjs/use-sync-external-store-extra.development.js +30.84% 3.07 kB 4.02 kB +21.15% 1.27 kB 1.54 kB
oss-stable/use-sync-external-store/cjs/use-sync-external-store-extra.development.js +30.84% 3.07 kB 4.02 kB +21.15% 1.27 kB 1.54 kB
oss-experimental/use-sync-external-store/cjs/use-sync-external-store-extra.production.min.js +27.91% 0.76 kB 0.97 kB +19.06% 0.47 kB 0.56 kB
oss-stable-semver/use-sync-external-store/cjs/use-sync-external-store-extra.production.min.js +27.91% 0.76 kB 0.97 kB +19.06% 0.47 kB 0.56 kB
oss-stable/use-sync-external-store/cjs/use-sync-external-store-extra.production.min.js +27.91% 0.76 kB 0.97 kB +19.06% 0.47 kB 0.56 kB

Generated by 🚫 dangerJS against c99f5ea

@acdlite acdlite force-pushed the use-ses-compare-to-rendered branch from b9f4f62 to c99f5ea Compare September 13, 2021 21:13
@acdlite acdlite merged commit fd5e01c into facebook:main Sep 13, 2021
Scheduler.unstable_yieldValue('Inline selector');
return [...state.items, 'C'];
};
const items = useSyncExternalStoreExtra(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, connectAdvanced

@markerikson
Copy link
Contributor

markerikson commented Sep 14, 2021

FYI, the shim can't actually use the built-in version atm, because it's not looking for the unstable_ prefix:

builtInAPI = React.useSyncExternalStore

And, uh... for that matter it's not even being exported from [email protected]? Now I'm confused. The implementation is there, but no export.

@rickhanlonii
Copy link
Member

Just following up: that issue was fixed, and we're checking for unstable now.

facebook-github-bot pushed a commit to facebook/react-native that referenced this pull request Sep 22, 2021
Summary:
This sync includes the following changes:
- **[f4ac680c7](facebook/react@f4ac680c7 )**: Fixed broken build script --unsafe-partial flag ([#22324](facebook/react#22324)) //<Brian Vaughn>//
- **[67222f044](facebook/react@67222f044 )**: [Experiment] Warn if callback ref returns a function ([#22313](facebook/react#22313)) //<Dan Abramov>//
- **[263cfa6ec](facebook/react@263cfa6ec )**: [Experimental] Add useInsertionEffect ([#21913](facebook/react#21913)) //<Ricky>//
- **[806aaa2e2](facebook/react@806aaa2e2 )**: [useSES shim] Import prefixed native API ([#22310](facebook/react#22310)) //<Andrew Clark>//
- **[fd5e01c2e](facebook/react@fd5e01c2e )**: [useSES/extra] Reuse old selection if possible ([#22307](facebook/react#22307)) //<Andrew Clark>//
- **[33226fada](facebook/react@33226fada )**: Check for store mutations before commit ([#22290](facebook/react#22290)) //<Andrew Clark>//
- **[86c7ca70a](facebook/react@86c7ca70a )**: Fix link ([#22296](facebook/react#22296)) //<Konstantin Popov>//
- **[0fd195f29](facebook/react@0fd195f29 )**: update error message to include useLayoutEffect or useEffect on bad e… ([#22279](facebook/react#22279)) //<salazarm>//
- **[8f96c6b2a](facebook/react@8f96c6b2a )**: [Bugfix] Prevent infinite update loop caused by a synchronous update in a passive effect ([#22277](facebook/react#22277)) //<Andrew Clark>//
- **[4ce89a58d](facebook/react@4ce89a58d )**: Test bad useEffect return value with noop-renderer ([#22258](facebook/react#22258)) //<Sebastian Silbermann>//
- **[a3fde2358](facebook/react@a3fde2358 )**: Detect subscriptions wrapped in startTransition ([#22271](facebook/react#22271)) //<salazarm>//

Changelog:
[General][Changed] - React Native sync for revisions 95d762e...e8feb11

jest_e2e[run_all_tests]

Reviewed By: rickhanlonii

Differential Revision: D30966369

fbshipit-source-id: 6c88e591005deb1fd93493628ef4695add49186c
zhengjitf pushed a commit to zhengjitf/react that referenced this pull request Apr 15, 2022
When you pass an unmemoized selector to useSyncExternalStoreExtra, we
have to reevaluate it on every render because we don't know whether
it depends on new props.

However, after reevalutating it, we should still compare the result
to the previous selection with `isEqual` and reuse the old one if it
hasn't changed.

Originally I did not implement this, because if the selector changes due
to new props or state, the component is going to have to re-render
anyway. However, it's still useful to return a memoized selection
when possible, because it may be the input to a downstream memoization.

In the test I wrote, the example I chose is selecting a list of
items from the store, and passing the list as a prop to a memoized
component. If the list prop is memoized, we can bail out.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed React Core Team Opened by a member of the React Core Team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants