-
-
Notifications
You must be signed in to change notification settings - Fork 573
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: implementation of useSyncExternalStore #291
Conversation
packages/react/hooks.ts
Outdated
// This implementation is grokked straight from Preact (ref: https://github.com/preactjs/preact/blob/528a7760c1cdec7515d514c71875860528e6dc12/compat/src/index.js#L142-L171) | ||
// (c) 2015-present Jason Miller | ||
export const useSyncExternalStore = (subscribe, getSnapshot) => { | ||
const [state, setState] = useState(getSnapshot); | ||
|
||
const value = getSnapshot(); | ||
|
||
useLayoutEffect(() => { | ||
if (value !== state) { | ||
setState(() => value); | ||
} | ||
}, [subscribe, value, getSnapshot]); | ||
const [{ instance }, forceUpdate] = useState({ | ||
instance: { value, getSnapshot } | ||
}); |
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.
It looks like preact's new useSyncExternalStore
is now ported from react's use-sync-external-store/shim
. So you should do it too.
Please do copy their comments describing how it breaks rule of hooks. Million.js doesn't have concurrent rendering (Both preact and Million.js performs synchronous rendering just like React pre-18), and it is the only reason why react's shim also works for Million.js (and preact).
Thanks for your contribution! I have just approved the CI. |
Pull Request Test Coverage Report for Build 3885856542
💛 - Coveralls |
Please describe the changes this PR makes and why it should be merged:
Status
Semantic versioning classification:
This PR updates the hook implementation used by
preact
.Refs: preactjs/preact#3663 and preactjs/preact@528a776