TypeError: dispatcher.useSyncExternalStore is not a function.

See original GitHub issue

Describe the bug

I hit this error:

TypeError: dispatcher.useSyncExternalStore is not a function. (In 'dispatcher.useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot)', 'dispatcher.useSyncExternalStore' is undefined)

What I have done:

// set up the react query hooks 
export const articlesToday = (
  params?: ArticlesTodayParams,
  signal?: AbortSignal
) => {
      return apiInstance<Article[]>(
      {url: `/articles/today/`, method: 'get', signal,
        params,
    },
      );
    }
  

export const getArticlesTodayQueryKey = (params?: ArticlesTodayParams,) => [`/articles/today/`, ...(params ? [params]: [])];

    
export type ArticlesTodayQueryResult = NonNullable<Awaited<ReturnType<typeof articlesToday>>>
export type ArticlesTodayQueryError = unknown

export const useArticlesToday = <TData = Awaited<ReturnType<typeof articlesToday>>, TError = unknown>(
 params?: ArticlesTodayParams, options?: { query?:UseQueryOptions<Awaited<ReturnType<typeof articlesToday>>, TError, TData>, }

  ):  UseQueryResult<TData, TError> & { queryKey: QueryKey } => {

  const {query: queryOptions} = options ?? {}

  const queryKey = queryOptions?.queryKey ?? getArticlesTodayQueryKey(params);

  

  const queryFn: QueryFunction<Awaited<ReturnType<typeof articlesToday>>> = ({ signal }) => articlesToday(params, signal);

  const query = useQuery<Awaited<ReturnType<typeof articlesToday>>, TError, TData>(queryKey, queryFn, queryOptions)

  return {
    queryKey,
    ...query
  }
}

// Wrap the app with QueryProvider 
const App = () => {
  const queryClient = new QueryClient();
  return (
    <QueryClientProvider client={queryClient}>
      <Provider>
        <CustomerAppNavigation />
      </Provider>
    </QueryClientProvider>
  );
};

export default App;

// Then inside the component using the hook like this

export interface ScreenProps {}

export function Screen(props: ScreenProps) {

   const { data } = useArticlesToday({ title: '123', ordering: '-created_at' });

    console.log(data);

   return (
            <Text key={article.uuid}>
              {`title = ${article.title}`}
            </Text>
   );
}

Above is my setup, then when I test the app by going to Screen in iOS and Android, I get this error

TypeError: dispatcher.useSyncExternalStore is not a function. (In 'dispatcher.useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot)', 'dispatcher.useSyncExternalStore' is undefined)

Here is the call stack I got in Expo Go. a12

It is cause by useSyncExternalStore and useBaseQuery which is from react-query package.

To solve this, I have read this issues https://github.com/TanStack/query/issues/3582, I at first in latest “react-query”: “4.0.0-beta.24”, Then I tried, beta.7, beta.10, beta.1 (as mention in the issue above) All having the same error.

Your minimal, reproducible example

n/a

Steps to reproduce

  • yarn add react-query@beta
  • Follow the minumum code example above.
  • Open in Expo Go

Note: no need a real API for this, the app just hitting the error once the Screen open.

Expected behavior

  • Hit the Screen
  • Will call API to URL
  • Will get result back from axios.data with `Article data inside

How often does this bug happen?

No response

Screenshots or Videos

a12

Platform

  • Expo Go
  • React native
  • Android and iOS both hitting the same error

react-query version

v4.0.0-beta.1, v4.0.0-beta.7, v4.0.0-beta.10, v4.0.0-beta.24

TypeScript version

~4.7.2

Additional context

The same setup Screen above running in Nextjs, having no problem, means that the behavior same exactly like the Expected behavior below

I am using

    "@expo/metro-config": "0.3.17",
    "@nrwl/next": "14.4.2",
    "@react-navigation/native": "^6.0.11",
    "@react-navigation/native-stack": "^6.7.0",
    "axios": "^0.27.2",
    "core-js": "^3.6.5",
    "expo": "45.0.6",
    "expo-dev-client": "~1.0.0",
    "expo-linking": "~3.1.0",
    "expo-splash-screen": "0.15.1",
    "expo-status-bar": "~1.3.0",
    "expo-structured-headers": "~2.2.1",
    "expo-updates": "~0.13.2",
    "native-base": "^3.4.9",
    "next": "12.1.6",
    "orval": "^6.8.1",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "react-native": "0.68.2",
    "react-native-gesture-handler": "~2.2.1",
    "react-native-reanimated": "~2.8.0",
    "react-native-safe-area-context": "4.2.4",
    "react-native-screens": "~3.11.1",
    "react-native-svg": "12.3.0",
    "react-native-svg-transformer": "1.0.0",
    "react-native-web": "0.17.7",
    "react-query": "4.0.0-beta.1",
    "regenerator-runtime": "0.13.7",
    "solito": "^0.0.26",
    "tslib": "^2.3.0"

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9

github_iconTop GitHub Comments

2reactions
joealdencommented, Jul 19, 2022

@kenchoong to give some more context on why the error occurs, it’ll be because v4 makes use of the useSyncExternalStore hook that was introduced in React 18, but (I believe) does so in a way where it keeps compatibility with react versions prior to 18 by detecting it’s existence in what react exports. Because you are depending on a react version that has the new hook exported, the compatibility check mentioned above succeeds, so it presumes that useSyncExternalStore can be used.

However, as you are depending on a react-native version that does not support React 18, the renderer doesn’t support the new hook under the hood. It’s like depending on react@18.x.x as well as react-dom@17.x.x - it just isn’t a supported case.

And while I understand that the current expo SDK version doesn’t yet officially support react-native@0.69.0, this isn’t a problem with this library, it’s a problem with your dependencies. So I think the real solution is to downgrade your react deps to 17.x.x until expo supports react-native@0.69.0+, but you could ignore the expo warning if you wanted to (but that may have other unforeseen problems).

1reaction
TkDodocommented, Aug 12, 2022

gonna close this for as I don’t think there is something we can or need to do.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Uncaught TypeError: dispatcher.useSyncExternalStore is not ...
This happens because react-redux is not supported yet with React 18 (React and React Native). Downgrade react-redux to the previous version ...
Read more >
TypeError: dispatcher.useSyncExternalStore is not a function
Hi all, I think I'm going crazy, because I've been trying to debug this issue with the apollo client for ~an hour, and...
Read more >
dispatcher.useSyncExternalStore is not a function как это ...
Uncaught TypeError: dispatcher.useSyncExternalStore is not a function как это решить? ; let mapStateToProps = (state) => ; state: state } } let ...
Read more >
invariant violation: viewproptypes has been removed from ...
At first I've received "TypeError: dispatcher.useSyncExternalStore is not a function." and fixed by updating react-native to "0.69.0-rc.0" version.
Read more >
React, how to fix the TypeError: resolver is not a function error
React, how to fix the TypeError: resolver is not a function error. Published in 2021. Psssst! The 2023 WEB DEVELOPMENT BOOTCAMP is starting...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found