Skip to content
Source
Sign inSign up
TanStack/query

Backwards-compatible shim for React.useSyncExternalStore does not work in react native 0.63 #5289

Send to my agent
Closedwx-shen(GitHub profile, opens in a new tab) opened this issue 3 comments

Describe 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

  1. if this is an issue with useSyncExternalStore shim, 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+

  2. so v5 do not suport react 17 ? i meet error React__namespace.useSyncExternalStore is not a function with react 17

    reproduce demo: https://codesandbox.io/s/prod-resonance-fj7xz6?file=/src/App.tsx

  3. so v5 do not suport react 17 ?

    yes

  4. TkDodo closed this

  5. TkDodo added labelsupstream

Sign in to join this conversation.