-
Notifications
You must be signed in to change notification settings - Fork 47.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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: #15315
Comments
I think you're facing the same issue as the folks in #13991 Could you have a look at some of the workarounds presented at the very bottom of the issue and see of they work for you? |
This happens if i use hook inside a HOC. const HOC = Component => {
return (props) => {
const [val] = useState();
return <div>{val}</div>
}
} |
@revskill10 please make a separate issue with a codesandbox repro. |
I will recommend you to use https://www.npmjs.com/package/webpack-bundle-analyzer. It happens we often forgot to link react and react-dom to parent application in case of libraries which add two builds of react and leads to this issue. If that's the case than just npm link react and react-dom to parent version of react and react-dom. |
Did you read this part? https://reactjs.org/warnings/invalid-hook-call-warning.html#duplicate-react It directly addresses the |
(Let me know if this doesn't help and we can reopen. I appreciate the detailed writeup.) |
Thanks, everyone for pitching in @gaearon I had already tried and retried resolving this according to the recommendations from the React page:
@threepointone funny enough so many people have found solutions to this at #13991 and I have spent the rest of yesterday implementing them with no success. |
Ok, if this didn’t help let’s leave it open so somebody can debug your issue and help you. The problem is the same (the way you link packages causes them to be duplicated in the bundle) but I’m not sure why the suggestion didn’t work for you. |
@gaearon you can now close it, the solution above worked. |
I just call hook in a very very basic way.
I get Error
|
@xeastcrast were you able to solve the issue? |
@carlosriveros If you get the same error after you check index.html You must remove script tag that src to your bundle in index.html Because HTMLWebpackPlugin will add script tag that include your bundle file automatically. |
Hi there, I am new to React Hooks. I just used Material-UI for creating Search Bar in my project. I wrote this code, but this is not working and giving the same issue. I read React Hooks docs but not got nothing regarding this.
PACKAGE.JSON
VERSION
EXPECTED RESULT
TRY |
I also meet this question when using material-ui. |
@stupidsongshu, so how you solved it? |
This happend to me today and I just run npm install --save react-dom@latest https://reactjs.org/warnings/invalid-hook-call-warning.html#duplicate-react |
I followed the instructions provided in the help page but it still didn't resolve the problem |
For me, we solved it for now by simply passing React around as a prop. This is an app and separate modules that we have full control of though, so I don't know if it applies to everyone. |
It didn't help me out too.. |
I have been testing all solutions about this hooks problem, any import of a component from an external lib using hooks will crash as there is 2 reacts instances (one from the lib, one from my app). I've followed all suggestions, nothing has been working so far. I'm gonna make component with a state until it gets clarified further ... |
Same Issue here. We have a Project with two react instances. One as |
I solve with removing |
I solve this by change
to
but I don't know why :( |
Fixed by using component instead of render by mistake. The official guide didn't mention this scenario. |
The "component" prop expects a React component, the "render" prop expects a function that will be invoked as a function. Function components are components, invoking them as normal functions doesn't work if they use hooks and generally isn't a good idea. |
Thanks, Dear, Its worked for me, Still, I am confused, why? |
@jaisonjjames see my reply. There are some explanations on StackOverflow if you have trouble understanding the difference: https://stackoverflow.com/questions/48150567/react-router-difference-between-component-and-render |
In my case, I had |
from to |
from <Route path="/component" component={myComponent} /> to <Route path="/component"><myComponent /></Route> -_- |
In my case I was referring to a library from the So I believe the reason I was getting this error was because |
@AbreezaSaleem How did you accomplish this? I'm having the same issue, using a library which has |
I created a somewhat minimal example to reproduce this. In my case the problem happens on ElectronJS with electron-webpack and react-dropzone |
there is a nice webpack configuration solution to pointing to the same/single react instance. It was to add a resolutions inside the app that is using the 'to be npm module'. inside webpack.config.js, add alias to react - so it uses that one react instance :
|
lerna can cause this problem, as I've learned (lerned?) today. lerna users, if you are developing a component library, do not include react in your library's dependencies, put it in your peerDependencies and @types/react in your devDependencies. |
For anyone using lerna, you may come across this problem when running tests in one package, where the code references components in another package. The problem that we experienced in this case was due to react being imported in the spec, and also being imported in a component in the component tree that was using Material UI's withStyles, which is implemented using hooks in Material UI. It seems that react internally manages state in a We were already using Craco to override Create React App's webpack config at build time:
However, this webpack override is only used at build time -- when running the tests, the code isn't built, but instantiated from source -- so our solution was to make use of CracoAlias in our
|
I get invalid Hook call problem only when i use material ui, with the number one possible reason being you might have mismatching versions of react and the renderer (such as React DOM). Its been happening every time for a month now and i have no clue on a solution. I've browsed through a lot on stackoverflow but no luck. |
Thanks a lot for saving my ass <3 |
For anyone else that end up here, I had the same issue after linking
After arriving #15315 (comment), my first test to confirm the linking issue by removing the link So now instead of copying and pasting I can use something like https://github.com/wclr/yalc |
I had this problem, and solved it with this: Not so sure on the why, but it was mainly a library using expo version of rn (react-native-web was the issue i think). |
Hello, everyone. import React, { useEffect } from "react";
import {useSelector} from "react-redux";
const myComponents = () => {
useEffect(()=>{
useSelctor(state => state.etc);
},[]);
return <></>;
}
export default myComponents; By writing the code like this, I got into the same issue. solving like this import React from "react";
import {useSelector} from "react-redux";
const myComponents = () => {
const { etc2 } = useSelctor(state => state.etc);
return <></>;
}
export default myComponents; If you encounter the error code as above, I recommend you to check how to use Hooks again. 🙇 |
just delete the node_module/react from your lib and try again. |
While @Yolantele's solution works when you only have a single peer dependency, you would need to do this for every peer dependency to make this work. const path = require('path');
const fs = require('fs');
// It would be best if you could just auto-resolve the folder names for the linked modules
const LINKED_FOLDER_NAMES = ['my-dep']
const alias = LINKED_FOLDER_NAMES
.map(linkedDependency => {
const rawdata = fs.readFileSync(`../${linkedDependency}/package.json`);
const json = JSON.parse(rawdata);
return Object.keys(json.peerDependencies || {}) || [];
})
.flat()
.reduce((acc, dependency) => ({
...acc,
[dependency]: path.resolve(`./node_modules/${dependency}`)
}), {}); Config for webpack
Config for gatsby
|
do you know how to fix this if you're not using typescript? |
Hi all, I am new to react and I am trying to create a react library of components and I came across this problem because one of the components I am creating uses REACT HOOKS.
Disclaimer: this is my first time creating an issue, so please bear with me.
So I am trying to create an accordion component which toggles between these classes
accordion__item--open
andaccordion__item
to open and close.package.json
webpack.config.js
This is the accordion container:
This is the accordion item that will live inside the container:
Now inside of a create-react-app I import these components
My library and the create-react-app are relative to each other and I am using
npm link
I have followed all of these instructions and I still get the same error.
Current behavior?
Steps to reproduce
cd react-lib
npm install
npm link
cd ../myapp
npm i
npm link react-lib
npm start
Expected behavior
accordion_item--open
should toggleTo see the above, do the following:
myapp/src/App.js
myapp/src/App.js
:Versions of React, Browser / OS are affected by this issue:
^16.8.6
on both react-lib and the myappVersion 0.61.52 Chromium: 73.0.3683.86 (Official Build) (64-bit)
Version 10.13.6 (17G5019)
The text was updated successfully, but these errors were encountered: