-
Notifications
You must be signed in to change notification settings - Fork 278
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 instead #2573
base: master
Are you sure you want to change the base?
Conversation
Size Change: +1.49 kB (+0.13%) Total Size: 1.17 MB
ℹ️ View Unchanged
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #2573 +/- ##
==========================================
+ Coverage 82.95% 82.98% +0.02%
==========================================
Files 444 444
Lines 9222 9237 +15
Branches 2254 2258 +4
==========================================
+ Hits 7650 7665 +15
Misses 1239 1239
Partials 333 333 ☔ View full report in Codecov by Sentry. |
d9b6440
to
2a7f072
Compare
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.
442c7c7
to
1c36b8c
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.