react: 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:
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 and accordion__item to open and close.
package.json
{
"name": "react-lib",
"version": "0.3.0",
"description": "A simple UI library of react components",
"main": "dist/index.js",
"publishConfig": {
"registry": ""
},
"scripts": {
"login": "",
"build": "webpack --mode=production",
"develop": "webpack --mode=development --watch"
},
"repository": {
"type": "git",
"url": ""
},
"keywords": [],
"author": "",
"license": "ISC",
"homepage": "",
"dependencies": {
"@babel/core": "^7.3.4",
"@babel/preset-env": "^7.3.4",
"@babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.5",
"eslint": "^5.15.1",
"eslint-loader": "^2.1.2",
"webpack": "^4.29.6",
"webpack-cli": "^3.2.3",
"webpack-node-externals": "^1.7.2"
},
"devDependencies": {
"eslint-config-airbnb": "^17.1.0",
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-jsx-a11y": "^6.2.1",
"eslint-plugin-react": "^7.12.4",
"eslint-plugin-react-hooks": "^1.6.0"
}
}
webpack.config.js
const path = require('path');
const webpack = require('webpack');
module.exports = {
mode: 'development',
optimization: {
minimize: true,
},
entry: './index.js',
output: {
path: path.resolve(__dirname, './dist'),
filename: 'index.js',
library: '',
libraryTarget: 'commonjs'
},
target: 'node',
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules)/,
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/react']
}
}
]
}
};
This is the accordion container:
import React from 'react';
function Accordion({ children }) {
return (
<div className="accordion">
{ children }
</div>
);
}
export default Accordion;
This is the accordion item that will live inside the container:
import React, { useState } from 'react';
function AccordionItem({header, content}) {
const [ isActive, toggleActive ] = useState(false);
return (
<div className={ `accordion__item ${ isActive ? 'accordion__item--open' : '' }` }>
<button
className="accordion__item-header"
onClick={ () => isActive ? toggleActive(false) : toggleActive(true) }
>
{ header }
<svg className="icon icon--debit" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50">
<path className="icon__path" d="M22.37,22.33V2.67a2.63,2.63,0,0,1,5.26,0V47.24a2.63,2.63,0,0,1-5.26.09V27.58H2.71a2.63,2.63,0,0,1,0-5.25Zm11.92,5.25a2.63,2.63,0,0,1,0-5.25h13a2.63,2.63,0,0,1,0,5.25Z">
</path>
</svg>
</button>
<div className="accordion__item-content">
{ children }
</div>
</div>
)
};
export default AccordionItem;
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
import React from 'react';
import {AccordionItem} from 'react-lib'
import {Accordion} from 'react-lib';
function App ({props}) {
return (
<Accordion>
<AccordionItem header={"Hello"} content={"World"}/>
</Accordion>
)
}
export default App;
I have followed all of these instructions and I still get the same error.
Current behavior?
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.
Steps to reproduce
- clone https://github.com/sethandleah/react-lib
- clone https://github.com/sethandleah/myapp
cd react-libnpm installnpm linkcd ../myappnpm inpm link react-libnpm start
Expected behavior
- It should show a button with a “plus” svg sign and the words “Hello” and “World” respectively
- Open devtools and go to elements
- When clicking on the button the class
accordion_item--openshould toggle
To see the above, do the following:
- Uncomment these lines at
myapp/src/App.js
import Accordion from './Accordion';
import AccordionItem from './AccordionItem';
- The comment out these line, alse at
myapp/src/App.js:
import { Accordion } from 'react-lib';
import { AccordionItem } from 'react-lib';
Versions of React, Browser / OS are affected by this issue:
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 82
- Comments: 70 (7 by maintainers)
Did you read this part?
https://reactjs.org/warnings/invalid-hook-call-warning.html#duplicate-react
It directly addresses the
linkworkflow:This happens if i use hook inside a HOC.
I solve this by change
to
but I don’t know why 😦
@carlosriveros Case 1: You might be check your index.html that only have one script tag bundle
If you get the same error after you check index.html Case 2: If you use Webpack html, In plugins property inside webpack.config file And not have {reject: true} in option of new HTMLWebpackPlugin();
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.
I just call hook in a very very basic way.
I get Error
Hooks can only be called inside of the body of a function component.I have create a lot of react project from sketch but I never get this error before. I understand all rules of hook. I spend a time for 12 hours to debug and solve it, But unfortunately I can’t, Now I have no idea.Fixed by using component instead of render by mistake. The official guide didn’t mention this scenario. Thanks, xyj404
<Route path="/login" exact component={LoginForm} />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 :
from
<Route path=“/component” render={myComponent} />
to <Route path=“/component” component={myComponent} />
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 …
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.currentvariable, 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 theInvalid hook call ...message.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
craco.config.jsto specify the react path during the tests: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.
from
to
-_-
I am also facing the same issue.
https://stackoverflow.com/questions/59173968/react-error-hooks-can-only-be-called-inside-the-body-of-a-function-component?noredirect=1#comment104570333_59173968
can you please guide me ?
I solve with removing
yarn.lock,package-lock.json,node_modules,dist, kind of cache and build files and then runyarnornpm installfor install libraries again. Now it works!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. REACTJS CODE
PACKAGE.JSON
VERSION
EXPECTED RESULT
TRY Try to run in CODESANDBOX, everything runs fine Check it.
You might have mismatching versions of React and React DOM. I had same issue solved by making change in React version
If you’re using lerna or a monorepo like me, add sth like this to your Webpack resolve config
If you’re getting it in Storybook, add it to custom Webpack config there too.
A side note, if this fixes the issue for you, your webpack built bundle also had 2 versions of react in it (use sth like webpack bundle analyzer to inspect it). In my case, I had a separate package in my monorepo that took care of bundling and all the tooling so I had to add an alias.
Yeah, I had same issue when I use @material-ui. It takes more time to figure out this issue. Then I searched google and found a solution for this. It’s here http://putridparrot.com/blog/react-material-ui-invalid-hook-call-hooks-can-only-be-called-inside-of-the-body-of-a-function-component/
Original post:
When looking through some examples of writing Material UI code within React and using TypeScript (within a .tsx file to be precise) you might come across an error at runtime such as “Invalid hook call. Hooks can only be called inside of the body of a function component”.
Here’s an example of the code which causes this error
What we need to do is wrap the useStyles code in a function and replace the code which uses it, so for example
This also shows how we can create reusable components from just a simple function.
=> It’s crazy but It’s working well. Hope this can help.
I followed the instructions provided in the help page but it still didn’t resolve the problem
I had the same problem but the cause was different. For anyone using auto-import plugins or snippets make sure you import from the same case react.
If you have mix of
import {useEffect} from 'react'andimport {useEffect} from 'React'you will run into this issue.I think you’re facing the same issue as the folks in https://github.com/facebook/react/issues/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?
just delete the node_module/react from your lib and try again.
Thanks a lot for saving my ass ❤️
In my case I was referring to a library from the
windowobject which had a different React version. I ended up using theexternalsconfiguration option from webpack which helped me refer to that library’sreactandreact-domand 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 had
let match = useRouteMatch("/user/:id");outside the component, which is a hook from react-router, so the error was right.Thanks, Dear, Its worked for me, Still, I am confused, why?
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.
This work like a charm!
Thank you, that’s solved my problem, I installed another React in my
docsdictory, which caused this problem.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