Skip to content

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

@hugmouse

Description

@hugmouse

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]);
// ...
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions