-
Notifications
You must be signed in to change notification settings - Fork 280
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: change useStateStore to use useSyncExternalStore #2573
base: master
Are you sure you want to change the base?
Conversation
Size Change: +1.43 kB (+0.12%) Total Size: 1.19 MB
ℹ️ View Unchanged
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #2573 +/- ##
==========================================
+ Coverage 81.12% 81.15% +0.02%
==========================================
Files 448 448
Lines 9464 9479 +15
Branches 2312 2316 +4
==========================================
+ Hits 7678 7693 +15
Misses 1423 1423
Partials 363 363 ☔ View full report in Codecov by Sentry. |
897e7fd
to
eb4d195
Compare
src/store/hooks/useStateStore.ts
Outdated
@@ -1,7 +1,10 @@ | |||
import { useEffect, useState } from 'react'; | |||
import { useCallback, useMemo, useSyncExternalStore } from 'react'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To support older React versions, it's better to use the use-sync-external-store/shim
package. It also has a useSyncExternalStoreWithSelector
helper hook which will do selector and selection memoization for you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do I understand it well, that not using use-sync-external-store/shim
could lead to new GH issues of those, who are using React 16 @myandrienko ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the ones who have React version smaller than React 18.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's correct, you should use use-sync-external-store/shim
if you want to support react@<18
. It uses the "native" implementation on react@>=18
, so there's no real downside.
442c7c7
to
1c36b8c
Compare
1e97101
to
0977dfd
Compare
0977dfd
to
856eda3
Compare
856eda3
to
1b1fb35
Compare
🎯 Goal
Changing stores on the fly would keep previously calculated state for a bit before the effect would run to recalculate it - using
useSyncExternalStore
(thank you, @myandrienko) should alleviate this issue. Bothsubscribe
andgetSnapshot
functions required by the React hook are wrapped to allow for selector functionality,geSnapshot
requires the output to be cached so the wrapper reuses similar cache check mechanism assubscribeWithSelector
does internally.