Skip to content

[Typo]: Section "My subscribe function gets called after every re-render" of useSyncExternalStore documentation is incorrect #7707

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

Closed
hugmouse opened this issue Apr 1, 2025 · 2 comments

Comments

@hugmouse
Copy link
Contributor

hugmouse commented Apr 1, 2025

Summary

In the given context the code that is used in the example will break due to javascript execution flow. subscribe is used before it being defined - it throws Uncaught TypeError: subscribe is not a function.

Page

https://react.dev/reference/react/useSyncExternalStore#my-subscribe-function-gets-called-after-every-re-render

Details

The following code will throw Uncaught TypeError: subscribe is not a function. Fix: declare subscribe before passing it to the useSyncExternalStore.

function ChatIndicator({ userId }) {
const isOnline = useSyncExternalStore(subscribe, getSnapshot);
// ✅ Same function as long as userId doesn't change
const subscribe = useCallback(() => {
// ...
}, [userId]);
// ...
}

@rickhanlonii
Copy link
Member

Nice catch, want to submit a PR?

Ideally, all 3 would move the subscribe before uSES. The middle example, moving it to the module, should go above the component (not for syntax reasons, but convention).

@rickhanlonii
Copy link
Member

Fixed in #7711, thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants