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.

Bundled React library throws Invalid hook call error

See original GitHub issue

Bug report

What is the current behavior?

I get the following React error when trying to use my library built with Webpack

Error: 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://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
Screenshot 2021-07-26 at 15 54 22

If the current behavior is a bug, please provide the steps to reproduce.

I’ve created a simple library https://github.com/vasilev-alex/webpack-example which reproduces the above error. As soon, as I import a React hook in my lib I get the above error.

What is the expected behavior?

I can use Webpack to bundle React libraries.

Other relevant information: webpack version: 5.46 Node.js version: v12.22.1 Operating System: macOS Big Sur Additional tools: -

I also posted this comment https://github.com/facebook/react/issues/13991#issuecomment-886656170 in the respecting issue https://github.com/facebook/react/issues/13991, none of the suggested solutions helped me.

P.S. using Rollup for the same library works fine.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
alexander-akaitcommented, Aug 4, 2021

hm, looks like different react versions, give me time I will look at this deeply

0reactions
alexander-akaitcommented, Aug 4, 2021
environment: {
  module: true,
},

You need this due https://github.com/vasilev-alex/webpack-example/blob/main/package.json#L21 and your browsers doesn’t support ECMA modules, when you bundle library you don’t need this, most of developers uses bundlers so imports will be bundled, for direct usage ECMA modules should be supports in browser, so I recommend to remove it, if you have app and library in one repo - you can use monorepo or solution from above

Read more comments on GitHub >

github_iconTop Results From Across the Web

React hooks in react library giving Invalid hook call error
Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for ...
Read more >
Invalid Hook Call Warning - React
Invalid Hook Call Warning. You are probably here because you got the following error message: Hooks can only be called inside the body...
Read more >
Solving React Hooks' invalid hook call warning - Rob Kendal
Building a React project and have the error 'Hooks can only be called inside the body of a function component'? We'll solve the...
Read more >
Invalid hook call error when I use my own library from styled ...
I created my own UI tool from styled-components library (using typescript and rollup). For example, I have the following component: import ...
Read more >
Form giving error : 'Error: Invalid hook call. Hook
Topic: Form giving error : 'Error: Invalid hook call. ... import React from 'react'; ... i downloaded the npm folder "MDB-React-Super-Bundle".
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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 Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found