"Invalid hook call." error while using Query
or useQuery
with react-apollo 3
#3454
Comments
@osenvosem Have you double checked |
@hwillson |
+1 Same behavior with |
I've just started seeing the same thing after upgrading to expo sdk35 (React 16.8.3) :( Interestingly |
I have a freshly installed project with create-react-app and see this problem too. |
In our case the issue was due to our umbrella app containing different react versions and I couldn't figure out how to get everything to use the right react versions. In the end we pinned all react versions to the one required by the react native app. |
Hi @tonnenpinguin can you post working |
@Kif11 it pretty much boils down to setting a specific version for react
|
+1 for this, I'm having the same issue as well ( minimal example project ): import React from "react";
import ReactDOM from "react-dom";
import {ApolloProvider, useQuery} from "@apollo/react-hooks";
import gql from "graphql-tag";
import {InMemoryCache} from "apollo-cache-inmemory";
import {HttpLink} from "apollo-link-http";
import {ApolloClient} from "apollo-client";
const cache = new InMemoryCache();
const link = new HttpLink({
uri: 'http://localhost:9001/'
});
const client = new ApolloClient({
cache,
link
});
const GET_USERS = gql`
query GetUsers {
getUsers {
id
}
}
`;
const Test = () => {
const {data} = useQuery(GET_USERS);
console.log(data);
return null
};
ReactDOM.render(<ApolloProvider client={client}><Test/></ApolloProvider>, document.getElementById("app")); Gets the error:
Here is my {
"name": "testname",
"productName": "testname",
"version": "0.0.12",
"scripts": {
"dev": "electron-webpack dev",
"compile": "electron-webpack"
},
"build": {
"appId": "com.testname.testname",
"productName": "testname",
"copyright": "Copyright © 2019 testname",
"publish": {
"provider": "s3",
"bucket": "testname"
},
"win": {
"target": "nsis"
},
"mac": {}
},
"dependencies": {
"@apollo/react-components": "^3.1.3",
"@apollo/react-hoc": "^3.1.3",
"@apollo/react-hooks": "^3.1.3",
"@reach/router": "^1.2.1",
"apollo-cache": "^1.3.2",
"apollo-cache-inmemory": "^1.6.2",
"apollo-client": "^2.6.3",
"apollo-link": "^1.2.13",
"apollo-link-http": "^1.5.15",
"apollo-utilities": "^1.3.2",
"electron-log": "^3.0.8",
"electron-updater": "^4.1.2",
"graphql": "^14.4.2",
"graphql-tag": "^2.10.1",
"react": "16.11.0",
"react-dom": "16.11.0",
"react-scripts": "^3.2.0",
"source-map-support": "^0.5.16",
"spectre.css": "^0.5.8"
},
"devDependencies": {
"@babel/cli": "^7.6.4",
"@babel/core": "^7.6.4",
"@babel/preset-env": "^7.6.3",
"@babel/preset-react": "^7.6.3",
"@graphql-codegen/cli": "^1.8.2",
"@graphql-codegen/introspection": "1.8.2",
"@graphql-codegen/typescript": "1.8.2",
"@graphql-codegen/typescript-operations": "^1.8.2",
"@graphql-codegen/typescript-react-apollo": "1.8.2",
"@types/reach__router": "^1.2.6",
"@types/react": "^16.9.11",
"@types/react-dom": "^16.9.3",
"babel-core": "^6.26.3",
"babel-loader": "^8.0.6",
"babel-preset-es2015-node": "^6.1.1",
"babel-preset-react": "^6.24.1",
"css-loader": "^3.2.0",
"electron": "5.0.6",
"electron-builder": "^21.0.11",
"electron-webpack": "^2.7.4",
"eslint": "^6.6.0",
"eslint-plugin-react-hooks": "^2.2.0",
"standard": "^14.3.1",
"standard-loader": "^7.0.0",
"style-loader": "^1.0.0",
"ts-loader": "^6.2.1",
"tslint": "^5.20.0",
"tslint-config-standard": "^8.0.1",
"tslint-loader": "^3.5.4",
"typescript": "^3.6.4",
"webpack": "^4.41.2",
"webpack-bundle-analyzer": "^3.6.0",
"webpack-cli": "^3.3.9",
"webpack-dev-server": "^3.9.0"
}
} |
After some testing, I've narrowed it down to a single variable:
works as expected.
Fails with the Invalid Hooks error. @hwillson thoughts? |
I am having the same issue inside yarn workspaces/lerna where all the apollo logic is within a I tried running the native app detached from lerna/workspaces but still have the error. I also use the exact same code and versions to create the client/provider.
Regular react hooks seem to work fine in the native app, using hooks directly without my own |
@klaaz0r I'm using One thing I might be doing different from you is using the If you haven't tried it, this option allows you to prevent dependencies from being shared across workspaces. In my case, I chose to just completely disable the hoisting feature using this in my root {
"workspaces": {
"packages": [
"projects/*"
],
"nohoist": [
"**"
]
},
} |
Issue still exists. import React, { useState } from 'react';
import {
FormGroup,
Label,
Input,
Container,
Button,
} from 'reactstrap';
import { useQuery } from '@apollo/react-hooks';
import {
ADD_USER,
} from '../Queries';
const Userform = (prop) => {
const [user, setUser] = useState({
name: '',
email: '',
job_title: ''
});
const updateUser = (e) => {
setUser({
...user,
[e.target.id]: e.target.value
});
}
const SaveUser = () => {
let result = useQuery(ADD_USER, {
variables: {
...user
}
});
};
return (
<Container>
<FormGroup>
<Label for="name">Name: </Label>
<Input type="text" id="name" onChange={updateUser} />
</FormGroup> <br/>
<FormGroup>
<Label for="email">E-mail: </Label>
<Input type="email" id="email" onChange={updateUser} />
</FormGroup> <br/>
<FormGroup>
<Label for="job-title">Job Title: </Label>
<Input type="text" id="job_title" onChange={updateUser} />
</FormGroup>
<Button onClick={SaveUser}>Save</Button>
</Container>
);
}
export default Userform; it errors on the with the version
|
I got same issue too with both Error:
|
If this helps anyone I was working with Gatsby and had the same error, but looking in the documentation found this:
I had different output in the |
same issue...please help... Action
Reducer
|
Same problem here. I checked the
Why is this closed? |
Running into this same issue, some solution/guidance would be appreciated |
Same here. Ends up with an error though, no warnings
|
Same issue. render() {
const { loading, error, data } = useQuery(Graphs.LIST_DEVICES);
if (loading) return <p>Loading...</p>;
if (error) return <p>Error :(</p>;
|
@isaiahtaylor @sebastienfi @Knaackee try to install @apollo/react-hooks to package not in the root I have the same problem with monorepo |
@isaiahtaylor You are "breaking the Rules of Hooks". https://reactjs.org/docs/hooks-rules.html#only-call-hooks-from-react-functions You shouldn't be using hooks inside a |
@sebastienfi To go along with what @Whispers12 said, the |
@Whispers12 already tried that with no success |
@traviswimer i am using a functional component |
@traviswimer I don't use yarn workspaces. I have all my modules in my package.json of my project |
@traviswimer you are correct! That was my issue. Thank you. |
@Knaackee My comment wasn't directed at you, but to answer your original question, I think this was closed because its more likely to be a react issue like the error suggests. I don't really know what to suggest other than the normal silly node fixes like deleting You might have better luck if you can post a question with some code on Stackoverflow. |
Still having this issue |
Some solution? I have same issue in monorepo with react native. React hooks work fine like useEffect or useState |
I also have the same problem with monorepo and react native.. |
I fixed it, yarn workspace @your/workspace add --dev babel-plugin-module-resolver in babel.config.js module.exports = {
// ...
plugins: [
[
'module-resolver',
{
root: ['./mobile'],
extensions: ['*'],
alias: {
react: './node_modules/react',
},
},
],
// ...
],
}; |
Hey, I'm having this issue only when trying to test. I am using react-native, apollo, graphql-code-generator and typescript. I am trying to get my tests set up with jest and @testing-library/react-native. The tests work except when I try to test a file using one of the ApolloReactHooks I get this error.
I am calling it in a function component along side other hooks that are working just fine. I tried all the suggestions in this thread and others around getting the invalid hook error and I have run out of ideas. Again this is working when I run my dev server or a production build. It just is not working when I run tests. Any thoughts or ideas would be appreciated. Test:
Component:
babel.config.js:
jest.config.js:
Oh yea and my apollo version:
|
Same issue |
Intended outcome:
Work without errors.
Actual outcome:
How to reproduce the issue:
I have an Electron + typescript app that just renders one component.
Version
"react-apollo": "3.0.1",
"react": "^16.9.0",
"react-dom": "^16.9.0",
"apollo-client": "^2.6.3",
The text was updated successfully, but these errors were encountered: