question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hooks + multiple instances of React

See original GitHub issue

To 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:open
  • Created 5 years ago
  • Reactions:314
  • Comments:449 (14 by maintainers)

github_iconTop GitHub Comments

1124reactions
apieceofbartcommented, Nov 3, 2018

I had the same issue and I resolved it by adding:

 alias: {
        react: path.resolve('./node_modules/react')
      }

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

476reactions
GabrielBBcommented, Nov 1, 2018

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found