uploadcare / react-widget

Uploadcare React Widget
MIT License
85 stars 18 forks source link

Invalid hook call #246

Open rikhav-parsegon opened 3 years ago

rikhav-parsegon commented 3 years ago

Using the Widget throws an invalid hook call error.

There are two errors that appear in the console:

Uncaught 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.
    at resolveDispatcher (react.development.js?aef8:1476)
    at useLayoutEffect (react.development.js?aef8:1523)
    at Config (index-be67ac8a.js?6244:187)
    at renderWithHooks (react-dom.development.js?61bb:14803)
    at mountIndeterminateComponent (react-dom.development.js?61bb:17482)
    at beginWork (react-dom.development.js?61bb:18596)
    at HTMLUnknownElement.callCallback (react-dom.development.js?61bb:188)
    at Object.invokeGuardedCallbackDev (react-dom.development.js?61bb:237)
    at invokeGuardedCallback (react-dom.development.js?61bb:292)
    at beginWork$1 (react-dom.development.js?61bb:23203)

Uncaught 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.
    at resolveDispatcher (react.development.js?aef8:1476)
    at useState (react.development.js?aef8:1507)
    at ClientSuspense (index.js?506e:5)
    at renderWithHooks (react-dom.development.js?61bb:14803)
    at mountIndeterminateComponent (react-dom.development.js?61bb:17482)
    at beginWork (react-dom.development.js?61bb:18596)
    at HTMLUnknownElement.callCallback (react-dom.development.js?61bb:188)
    at Object.invokeGuardedCallbackDev (react-dom.development.js?61bb:237)
    at invokeGuardedCallback (react-dom.development.js?61bb:292)
    at beginWork$1 (react-dom.development.js?61bb:23203)

This occurred when integrating uploadcare into our existing React app, following step 2 here

To reproduce, run

npm install @uploadcare/react-widget

and include

import { Widget } from "@uploadcare/react-widget";

const SomeComponentInYourApp = () => {
    return <p>
                    <label htmlFor="file">Your file:</label>{" "}
                    {/* @ts-ignore */}
                    <Widget publicKey="demopublickey" id="file" />
                </p>;
}

Environment

nd0ut commented 3 years ago

Hey @rikhav-parsegon! Thanks for report.

Unfortunately, I can't reproduce the problem. It seems that you have > 1 react instances in your project.

Could you provide npm ls react or yarn list react output?

rikhav-parsegon commented 3 years ago

npm ls react is printing

@parsegon/js@1.0.0 /Users/rikhav/Desktop/frontend/app
├─┬ @uploadcare/react-widget@1.3.5
│ ├─┬ @uploadcare/client-suspense@1.0.5
│ │ └── react@16.14.0 deduped
│ └── react@16.14.0 deduped
└── react@16.14.0

On a hunch, I disabled code splitting by adding the plugin new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 }) to our config, and that seems to have fixed the issue.