question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invalid hook call when used inside React Native

See original GitHub issue

Feels like I’m going crazy about this issue. I’m currently using @apollo/client with react-native in a yarn workspace. I’ve setup the yarn workspace correctly with the custom metro config required, and the nohoist options. Here are my configs:

 "workspaces": {
    "nohoist": [
      "@react-native-community/async-storage",
      "react-native",
      "react-native/**",
      "react-native-dev-menu",
      "react-native-svg",
      "jetifier",
      "react-native-gesture-handler",
      "@apollo/client",
      "react"
    ]
  },
  "resolutions": {
    "@types/react": "^17",
    "react": "17.0.2",
    "@apollo/client": "3.3.21"
  },
  "dependencies": {
    "react": "17.0.2",
    "@apollo/client": "3.3.21"
    // ...
  }
const getWorkspaces = require("get-yarn-workspaces");
const path = require("path");

function getConfig(appDir) {
  const workspaces = getWorkspaces(appDir);

  const watchFolders = [
    ...workspaces.filter((workspaceDir) => !(workspaceDir === appDir)),
    path.resolve(appDir, "node_modules"),
    path.resolve(appDir, "..", "node_modules"),
  ];

  return {
    watchFolders,
    resolver: {
      extraNodeModules: {
        "react-native": path.resolve(appDir, "node_modules", "react-native"),
        react: path.resolve(appDir, "node_modules", "react"),
        "react-native-svg": path.resolve(appDir, "node_modules", "react-native-svg"),
        "core-js": path.resolve(appDir, "node_modules", "core-js"),
        "@apollo/client": path.resolve(appDir, "node_modules", "@apollo", "client"),
      },
    },
  };
}

module.exports = getConfig(__dirname);

This works as expected, and normal React hooks works. But using useQuery gives me:

Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.

This error is located at:
    in AppInner (at App.tsx:44)
    in ApolloProvider (at App.tsx:43)
    in App (at renderApplication.js:47)
    ...

My application code is:

import { AppRegistry } from "react-native";
import { App } from "./src/components/App";
import { name as appName } from "./app.json";
import "react-native-gesture-handler";

AppRegistry.registerComponent(appName, () => App);
import React, { useCallback, useContext, useEffect } from "react";
import { Text } from "react-native";
import {
  ApolloClient,
  ApolloProvider,
  useQuery,
} from "@apollo/client";
import { useQueryHookFromOtherPackage } "@propty/code/src/generated"

const client = new ApolloClient({
  // ...
});

export const App: React.FC = () => {
  return (
    <ApolloProvider client={client}>
      <AppInner />
    </ApolloProvider>
  );
};

const AppInner: React.FC = () => {
  // This hook is generated using GraphQL code generation in a package inside our yarn workspace
  useQueryHookFromOtherPackage()

  return <Text>test</Text>

It seems like it’s specifically the useQuery hook that breaks, as other hooks there works fine.

I’ve checked and @apollo/client and react are also properly hoisted, meaning they should not be any weird version issues.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
mangkorancommented, Jan 17, 2022

Currently using @apollo/client@3.5.6. Still getting the same error as OP.

0reactions
Cretezycommented, Jul 25, 2021

I’ve found the source of the problem.

My current yarn workspace looks like:

  • /: The root
    • core: Where the Apollo Hooks are generated
    • mobile: The React Native applications that’s problematic
    • web

Inside mobile/package.json, I have:

 "workspaces": {
    "nohoist": [
      "**/*"
    ]
  },

And in /package.json, I have:

  "workspaces": {
    "packages": [
      "web",
      "mobile",
      "core"
    ],
    "nohoist": [
      "**/react",
      "**/react/**",
      "**/@apollo/client",
      "**/@apollo/client/**"
    ]
  },

The problem

My core package has @apollo/client as a devDependency (mobile also has it). When I remove @apollo/client from the core’s devDependency, I can use the hooks correctly.

This doesn’t seem to be strictly a problem with @apollo/client, but more of a Metro/React Native issue.

No matter what I do, as long as @apollo/client is in the devDependencies, this error appears.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Invalid hook call. Hooks can only be called inside of the body ...
Invalid hook call. Hooks can only be called inside of the body of a function component · You might have mismatching versions of...
Read more >
Invalid hook call. Hooks can only be called inside the body of ...
Invalid hook call. Hooks can only be called inside the body of a function component # · Having a mismatch between the versions...
Read more >
Invalid hook call - How to resolve multiple versions of React ...
You'll want to first ensure references to react and react-dom in the library are in the peerDependencies. (NOTE: use the version you have ......
Read more >
Invalid Hook Call, inside react-native-picker-select · Issue #486
Invalid Hook Call error inside react-native-picker-select node module. ... See https://reactjs.org/link/invalid-hook-call for tips about how to ...
Read more >
Invalid hook call. Hooks can only be called inside of the body ...
Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of...
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