Open darwin808 opened 2 years ago
@agilgur5 added steps to reproduce
I just stumbled across this issue while googling same problem for other library. No idea why this is happening, but if you include your lib as file dependency in package.json
it works. (And maybe you'll need to remove react from dev deps of your library)
"dependencies": {
"react-local-toast": "file:../../react-local-toast"
},
My guess is:
Since you have react in dev deps of your library (probably), your library uses their installation of react, instead of one from parent project. For some reason Next shows only Cannot read properties of null (reading 'useState')
but if you check console, there will be probably error saying that you did something wrong with hooks, and maybe you have two different react
running.
I couldn't get it working with link
unfortunately. Even if I remove react from node_modules
of lib, I get different error. Switching to file dependency solved this problem
I just stumbled across this issue while googling same problem for other library. No idea why this is happening, but if you include your lib as file dependency in
package.json
it works. (And maybe you'll need to remove react from dev deps of your library)"dependencies": { "react-local-toast": "file:../../react-local-toast" },
My guess is:
Since you have react in dev deps of your library (probably), your library uses their installation of react, instead of one from parent project. For some reason Next shows only
Cannot read properties of null (reading 'useState')
but if you check console, there will be probably error saying that you did something wrong with hooks, and maybe you have two differentreact
running.I couldn't get it working with
link
unfortunately. Even if I remove react fromnode_modules
of lib, I get different error. Switching to file dependency solved this problem
Cannot read properties of null (reading 'useState')
It bothered me for many days and this method worked
npm link
with url to react used in other libraryThe one thing that helped me: https://stackoverflow.com/questions/56663785/invalid-hook-call-hooks-can-only-be-called-inside-of-the-body-of-a-function-com
I was able to solve this by hard coding the path to react and react dom in webpack via next.config.js
const nextConfig = {
webpack: (config, { dev }) => {
config.resolve.alias = {
...config.resolve.alias,
...(dev && {
react: `${config.context}/node_modules/react`,
"react-dom": `${config.context}/node_modules/react-dom`,
}),
};
return config;
}
};
THE DEFINITIVE SOLUTION TO THIS PROBLEM IS THIS -> https://iws.io/2022/invalid-hook-multiple-react-instances
Current Behavior
I keep getting this error in development only (when using
yarn link
), but in production (when published) everything is okExpected behavior
it should work no problems like it does in production
Step to reproduce
Suggested solution(s)
npm link ../${YOUR_NEXTJS_OR_REACT_FOLDER}/node_modules/react/
Additional context
-its working when i publish to npm then use that import
Your environment
-->