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