Hooks + multiple instances of React
See original GitHub issueTo people coming from search: please read this page first. It contains most common possible fixes!
Do you want to request a feature or report a bug?
Enhancement
What is the current behavior?
I had multiple instances of React by mistake.
When trying to use hooks, got this error:
hooks can only be called inside the body of a function component
Which is not correct since I was using function components. Took me a while to find the real cause of the issue.
What is the expected behavior?
Show the correct error message. Maybe detect that the app has multiple instances of React and say that it may be the reason of bugs.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:314
- Comments:449 (14 by maintainers)
Top Results From Across the Web
React Hooks: How can I use multiple instances of my ...
I've created a custom useToggle react hook and I want to use two instances in one component. A toggle for the "Privacy Policy"...
Read more >How to correct having (at least) two instances of React installed
How to correct having (at least) two instances of React installed ... Hooks can only be called inside of the body of a...
Read more >Invalid Hook Call Warning - React
Hooks can only be called inside the body of a function component. ... In general, React supports using multiple independent copies on one...
Read more >Invalid hook call - How to resolve multiple versions of React ...
I've recently figured out through testing (that you can link both react and react-dom at the same time). I noticed that whenever I...
Read more >Create custom fetch hook for multiple Axios instances - ITNEXT
It might be fun when you first start to convert your existing codebase to hooks or start to write your components from scratch...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Hashnode Post
No results found
I had the same issue and I resolved it by adding:
to
resolve
property in webpack config of my main app.It’s was obviously my mistake of using two copies of React but I agree that it would be great if the error message was better. I think this is maybe similar to: https://github.com/facebook/react/issues/2402
Yup, i tried to npm link a package i’m creating. It throws that same error since the other package is also using hooks but with its own React. I had to publish my package to NPM and then import it directly from NPM. That way the error was gone, but i hope this is fixed since publishing a package without testing it is bad, obviously