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

Fix useSyncExternalStore dropped update when state is dispatched in render phase #25578

Merged
merged 2 commits into from
Nov 8, 2022

Conversation

pandaiolo
Copy link
Contributor

@pandaiolo pandaiolo commented Oct 28, 2022

Summary

Fix #25565

In updateSyncExternalStore, If state is dispatched during render phase, then work in progress hook will hold already updated memoized state in the second pass, defeating previous value comparison. As a consequence, the effect updating internal store value with next value will not be scheduled.

This breaks re-rendering when store is set back to it's mounting value.

Using currentHook instead (when defined) fixes that issue.

How did you test this change?

See included test

If state is dispatched during render phase, then work in progress
`hook` will hold already updated memoized state, defeating previous
value comparison.

Using `currentHook` instead fixes that issue.
@sizebot
Copy link

sizebot commented Oct 28, 2022

Comparing: cf3932b...776bd87

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 = 152.77 kB 152.78 kB = 48.73 kB 48.73 kB
oss-experimental/react-dom/cjs/react-dom.production.min.js = 154.69 kB 154.70 kB = 49.34 kB 49.34 kB
facebook-www/ReactDOM-prod.classic.js = 526.93 kB 526.96 kB +0.01% 94.15 kB 94.16 kB
facebook-www/ReactDOM-prod.modern.js = 512.19 kB 512.22 kB +0.01% 91.98 kB 91.99 kB
facebook-www/ReactDOMForked-prod.classic.js = 526.93 kB 526.96 kB +0.01% 94.15 kB 94.16 kB

Significant size changes

Includes any change greater than 0.2%:

(No significant changes)

Generated by 🚫 dangerJS against 776bd87

@eps1lon eps1lon requested a review from acdlite October 28, 2022 10:41
Copy link
Collaborator

@eps1lon eps1lon left a comment

Choose a reason for hiding this comment

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

Nice test, thanks!

@eps1lon eps1lon changed the title Fix useSyncExternalStore when state is dispatched in render phase Fix useSyncExternalStore dropped update when state is dispatched in render phase Nov 8, 2022
@eps1lon eps1lon merged commit 1e3e30d into facebook:main Nov 8, 2022
facebook-github-bot pushed a commit to facebook/react-native that referenced this pull request Nov 10, 2022
Summary:
This sync includes the following changes:
- **[d1e35c703](facebook/react@d1e35c703 )**: Don't disappear layout effects unnecessarily ([#25660](facebook/react#25660)) //<Samuel Susla>//
- **[1e3e30dae](facebook/react@1e3e30dae )**: Fix useSyncExternalStore dropped update when state is dispatched in render phase ([#25578](facebook/react#25578)) //<Aurélien Chivot-Buhler>//

Changelog:
[General][Changed] - React Native sync for revisions 4bd245e...d1e35c7

jest_e2e[run_all_tests]

Reviewed By: GijsWeterings

Differential Revision: D41187776

fbshipit-source-id: 9c82b79458487191f4a26cf643321603d30cea5a
mofeiZ pushed a commit to mofeiZ/react that referenced this pull request Nov 17, 2022
OlimpiaZurek pushed a commit to OlimpiaZurek/react-native that referenced this pull request May 22, 2023
Summary:
This sync includes the following changes:
- **[d1e35c703](facebook/react@d1e35c703 )**: Don't disappear layout effects unnecessarily ([facebook#25660](facebook/react#25660)) //<Samuel Susla>//
- **[1e3e30dae](facebook/react@1e3e30dae )**: Fix useSyncExternalStore dropped update when state is dispatched in render phase ([facebook#25578](facebook/react#25578)) //<Aurélien Chivot-Buhler>//

Changelog:
[General][Changed] - React Native sync for revisions 4bd245e...d1e35c7

jest_e2e[run_all_tests]

Reviewed By: GijsWeterings

Differential Revision: D41187776

fbshipit-source-id: 9c82b79458487191f4a26cf643321603d30cea5a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants