Pluggable widgets react dependency error

I’ve tried to install the @lexical/react module in a Mendix pluggable widget I’m creating. However, I’m getting the following 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 for tips about how to debug and fix this problem.   I’ve kept the code as the default HelloWorldSample for now, so I know there aren’t invalid hook calls. It’s looking like there are multiple versions of react being installed though.  My dependency tree for react is: ├─┬ @lexical/react@0.6.4 │ ├─┬ react-dom@17.0.1 │ │ └── react@17.0.1 deduped │ ├─┬ react-error-boundary@3.1.4 │ │ └── react@17.0.1 deduped │ └── react@17.0.1 └─┬ @mendix/pluggable-widgets-tools@9.18.0   ├─┬ @testing-library/react@12.1.5   │ └── react@17.0.1 deduped   ├─┬ @wojtekmaj/enzyme-adapter-react-17@0.6.7   │ ├─┬ @wojtekmaj/enzyme-adapter-utils@0.1.4   │ │ └── react@17.0.1 deduped   │ └── react@17.0.1 deduped   ├─┬ jest-react-hooks-shallow@1.5.1   │ └── react@16.14.0   ├─┬ jest-svg-transformer@1.0.0   │ └── react@17.0.1 deduped   ├─┬ react-hot-loader@4.13.1   │ └── react@17.0.1 deduped   ├─┬ react-native@0.64.2   │ ├── react@17.0.1 deduped   │ └─┬ use-subscription@1.8.0   │   ├── react@17.0.1 deduped   │   └─┬ use-sync-external-store@1.2.0   │     └── react@17.0.1 deduped   ├─┬ react-test-renderer@17.0.1   │ ├─┬ react-shallow-renderer@16.15.0   │ │ └── react@17.0.1 deduped   │ └── react@17.0.1 deduped   └── react@17.0.1 deduped     I’ve spotted jest-react-hooks-shallow, a dependency from @mendix/pluggable-widgets-tools, uses react 16.14.0.  Is there anything I can do to get this using the same version (17.0.1) as the rest of the app?
1 answers

Usually this error really just is what the name suggests: You started with a React class component and attempt to use a hook. That is not possible. You need to use a function component to use hooks. Recent versions of the mendix widget generator allow you to choose between class or function components. Also, there is no problem in creating a separate function component and using it from a class component. However, probably best to create your widget using function components entirely.