Invalid hook call when used inside React Native
See original GitHub issueFeels 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:
- Created 2 years ago
- Comments:7 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Currently using
@apollo/client@3.5.6
. Still getting the same error as OP.I’ve found the source of the problem.
My current yarn workspace looks like:
/
: The rootcore
: Where the Apollo Hooks are generatedmobile
: The React Native applications that’s problematicweb
Inside
mobile/package.json
, I have:And in
/package.json
, I have:The problem
My
core
package has@apollo/client
as a devDependency (mobile
also has it). When I remove@apollo/client
from thecore
’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.