0

I created :

  • A reusable react component package, and
  • A react app for testing the component

Both of them are inside a monorepo (lerna). The problem is the "react" packages for the component & for the app are pointed to "different thing", thus using hook inside the component triggers this error: "Hooks can only be called inside the body of a function component".

Deleting the second "react" package directory will affect to the component project because it's a symbolic linking (similar to shortcut to component project). Any idea for resolving my problem?enter image description here

Heyyy Marco
  • 633
  • 1
  • 12
  • 22
  • If you're using webpack you can create an `alias` for `react` and `react-dom` inside `my-react-app` that points to ` – morganney May 14 '22 at 14:08
  • i'm using a react-script and and won't to eject it. – Heyyy Marco May 14 '22 at 14:14
  • Then try what it says at the bottom of this doc: https://reactjs.org/warnings/invalid-hook-call-warning.html. Link the react from your lib, to the react in your app. – morganney May 14 '22 at 14:16

1 Answers1

0
  1. clean all the node_modules.
  2. use peer dependencies in react component.

l use that two steps slove react version conflict problem in monorepo. You can try it.

baihaihui
  • 153
  • 1
  • 7