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.

React invalid hook call in library mode

See original GitHub issue

Describe the bug

I have a React component that is built using vite library mode. But when i import this component in app created by create-react-app i get the following error:

react.development.js:209 Warning: 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.
printWarning @ react.development.js:209
react.development.js:1630

Uncaught TypeError: Cannot read properties of null (reading 'useRef')
    at useRef (react.development.js:1630:1)
    at YN (Swipeout.tsx:59:1)
    at renderWithHooks (react-dom.development.js:16305:1)
    at mountIndeterminateComponent (react-dom.development.js:20074:1)
    at beginWork (react-dom.development.js:21587:1)
    at HTMLUnknownElement.callCallback (react-dom.development.js:4164:1)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:4213:1)
    at invokeGuardedCallback (react-dom.development.js:4277:1)
    at beginWork$1 (react-dom.development.js:27451:1)
    at performUnitOfWork (react-dom.development.js:26557:1)

Though it works fine in an app created by Vite

Reproduction

https://github.com/letoxyz/swipeout

Steps to reproduce

git clone https://github.com/letoxyz/swipeout
cd swipeout
npm install
npm run build
cd ..
npx create-react-app swipeout-test
cd swipeout-test
npm install
npm install ../swipeout
npm start

Insert the following code to swipeout-test/src/App.tsx:

import { Swipeout } from "@letoxyz/swipeout";
import "@letoxyz/swipeout/style.css";
import "./App.css";

function App() {
  return (
    <div className="App">
      <Swipeout
        {...{
          children: <div>Test</div>,
          actions: {
            left: [
              {
                renderContent: () => (
                  <div>
                    Red <br /> action
                  </div>
                ),
                width: 86,
                background: "#ff3737",
                onTrigger: () => console.log("red triggered"),
              },
            ],
            right: [
              {
                renderContent: () => (
                  <div>
                    Yellow <br /> action
                  </div>
                ),
                background: "#ffe437",
                width: 86,
                onTrigger: () => console.log("yellow triggered"),
              },
            ],
          },
        }}
      />
    </div>
  );
}

export default App;

System Info

System:
    OS: macOS 12.6
    CPU: (8) arm64 Apple M1 Pro
    Memory: 127.38 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
Binaries:
    Node: 16.15.0 - ~/Library/Caches/fnm_multishells/61188_1669370394724/bin/node
    npm: 8.5.5 - ~/Library/Caches/fnm_multishells/61188_1669370394724/bin/npm
Browsers:
    Firefox Developer Edition: 108.0
    Safari: 15.6.1

Used Package Manager

npm

Validations

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
luo3housecommented, Dec 2, 2022

I am sorry I ignored an important operation.

And I don’t think it is the issue of Vite.

The repo uses Rollup instead also causes the problem “Invalid hook call” if I follow steps of reproduction wrote above.

Please visit my repo related this issue if you are still in this problem, and try following README.md

0reactions
mlshvcommented, Dec 2, 2022

Looks like the issue happens because of linking the local version of a library in playground project to speed up the development. It leads to “duplicate version of React” because of a symlink to a library directory which has node_modules inside. This is where another copy of React sits

Thank you for your time, there is really no problem here

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
There are three common reasons you might be seeing it: You might have mismatching versions of React and React DOM. You might be...
Read more >
Invalid hook call - How to resolve multiple versions of React ...
i.e. npm i react@17 . From here on out, I'm going to call the custom package library and the application consuming it app,...
Read more >
React library(lib mode) doesn't work with Vite: Invalid hook call
The cause of 'Invalid hook call' error is that you use two react and react-dom module. The first one is bundled from node_modules,...
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 >

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