Closed sethandleah closed 5 years ago
I solve this by change
<Route key={index} render={route.component} path={route.path} />
to
<Route key={index} component={route.component} path={route.path} />
but I don't know why :(
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 let match = useRouteMatch("/user/:id");
outside the component, which is a hook from react-router, so the error was right.
from
<Route path="/component" render={myComponent} />
to <Route path="/component" component={myComponent} />
from
<Route path="/component" component={myComponent} />
to
<Route path="/component"><myComponent /></Route>
-_-
In my case I was referring to a library from the window
object which had a different React version. I ended up using the externals
configuration option from webpack which helped me refer to that library's react
and react-dom
and made the error go away.
So I believe the reason I was getting this error was because You might have more than one copy of React in the same app
.
In my case I was referring to a library from the
window
object which had a different React version. I ended up using theexternals
configuration option from webpack which helped me refer to that library'sreact
andreact-dom
and made the error go away.So I believe the reason I was getting this error was because
You might have more than one copy of React in the same app
.
@AbreezaSaleem How did you accomplish this? I'm having the same issue, using a library which has react
as an external is somehow showing up as a different version... I've tried externals: [ 'react' ]
, externals: { react: 'react' }
and externals: { react: 'React' }
, result is always the same...
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 :
module.exports = {
resolve: {
alias: {
react: path.resolve('./node_modules/react')
}
},
// other webpack settings
}
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 ReactCurrentDispatcher.current
variable, and this ends up getting set in one instance of react, but used in the other instance of react -- when it's empty, it throws the Invalid hook call ...
message.
We were already using Craco to override Create React App's webpack config at build time:
webpack: {
alias: {
react: path.resolve(__dirname, './node_modules/react'),
},
},
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 craco.config.js
to specify the react path during the tests:
plugins: [
{
plugin: CracoAlias,
options: {
source: 'options',
baseUrl: './',
aliases: {
// We need to alias react to the one installed in the desktop/node_modules
// in order to solve the error "hooks can only be called inside the body of a function component"
// which is encountered during desktop jest unit tests,
// described at https://github.com/facebook/react/issues/13991
// This is caused by two different instances of react being loaded:
// * the first at packages/desktop/node_modules (for HostSignUpDownloadComponent.spec.js)
// * the second at packages/components/node_modules (for packages/components/Modal)
react: './node_modules/react',
},
},
},
],
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.
My library and the create-react-app are relative to each other and I am using npm link
Did you read this part?
https://reactjs.org/warnings/invalid-hook-call-warning.html#duplicate-react
It directly addresses the
link
workflow:
Thanks a lot for saving my ass <3
For anyone else that end up here, I had the same issue after linking @ui-lib
with projectA
.
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://fb.me/react-invalid-hook-call for tips about how to debug and fix this problem.
After arriving https://github.com/facebook/react/issues/15315#issuecomment-479802153, my first test to confirm the linking issue by removing the link yarn unlink @ui-lib
in projectA
and yarn install --force
then copying the dist
folder from @ui-lib
into the projectA/node_modules/@ui-lib/dist
and everything worked as expected.
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: https://github.com/facebook/react/issues/13991#issuecomment-830308729
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. I hope no one makes the same mistake as me and leave this comment.
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. In my case, the problem was not the version issue of react, but the incorrect way Hooks called.
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
webpack: {
alias,
},
Config for gatsby
exports.onCreateWebpackConfig = ({ stage, actions }) => {
actions.setWebpackConfig({
resolve: { alias }
});
};
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. @types/react in your devDependencies.
do you know how to fix this if you're not using typescript?
I am unable to get console of inside my useEffect but i am getting console outside my useEffect
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:
Uncomment these lines at
myapp/src/App.js
The comment out these line, alse at
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)