Backwards-compatible shim for React.useSyncExternalStore does not work in react native 0.63 #5289
Send to my agentDescribe the bug#
observer.subscribe is registered in useSyncExternalStore.
useSyncExternalStore(
React.useCallback(
(onStoreChange) =>
isRestoring
? () => undefined
: observer.subscribe(notifyManager.batchCalls(onStoreChange)),
[observer, isRestoring],
),
() => observer.getCurrentResult(),
() => observer.getCurrentResult(),
)
However, in react-native, the polyfill of useSyncExternalStore does not work as expection. There is not dom api in react-native, so the polyfill finally points to useSyncExternalStoreShimServer.js, which did not handle subscribe.
export function useSyncExternalStore<T>(
subscribe: (() => void) => () => void,
getSnapshot: () => T,
getServerSnapshot?: () => T,
): T {
// Note: The shim does not use getServerSnapshot, because pre-18 versions of
// React do not expose a way to check if we're hydrating. So users of the shim
// will need to track that themselves and return the correct value
// from `getSnapshot`.
return getSnapshot();
}
As a result, there is no listeners pushed into listeners of queryObserver instance, and the queryFn will not execuate.
subscribe(listener: TListener): () => void {
this.listeners.push(listener as TListener)
this.onSubscribe()
return () => {
this.listeners = this.listeners.filter((x) => x !== listener)
this.onUnsubscribe()
}
}
Your minimal, reproducible example#
https://codesandbox.io/s/github/tanstack/query/tree/main/examples/react/simple?from-embed
Steps to reproduce#
Because of there is no builditApi or polyfill of useSyncExternalStore in react-native 0.63.0(with react version 16.13.1) , the queryFn does not execuate.
Expected behavior#
QueryFn execuate as usual.
How often does this bug happen?#
Every time
Screenshots or Videos#
No response
Platform#
OS: Android & iOS react-query version: 4.29.1
Tanstack Query adapter#
react-query
TanStack Query version#
4.29.1
TypeScript version#
4.9.5
Additional context#
No response
so
v5do not suportreact 17? i meet errorReact__namespace.useSyncExternalStore is not a functionwithreact 17reproduce demo: https://codesandbox.io/s/prod-resonance-fj7xz6?file=/src/App.tsx
so v5 do not suport react 17 ?
yes
TkDodo closed this
TkDodo added labelsupstream
Sign in to join this conversation.
if this is an issue with
useSyncExternalStoreshim, I think it would be better to file an issue directly with react. Lots of libraries, e.g. redux, are also using the shim.FYI, we've removed the shim in v5 because we're depending on React18+