react: Hooks don't work with yarn link
Do you want to request a feature or report a bug? Bug
What is the current behavior?
When developing an external library locally and using yarn link to link the library to a local react app the “hooks can only be called inside the body of a function component” error comes up. However, after publishing to npm and using the published version in the local react app everything works as expected.
If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn’t have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:
- Create a library that’s built with hooks (my-hooks-lib)
- Create a local app that uses the library (my-react-app) using CRA
yarn linkin my-hooks-lib and in my-react-app runyarn link my-hooks-lib
What is the expected behavior?
yarn start in the react app should use hooks and render normally
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
react and react-dom 16.7.0-alpha.2
OSX
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 38
- Comments: 38
Commits related to this issue
- added yalc due to https://github.com/facebook/react/issues/14257 — committed to bennoinbeta/dynamic-styles by bennoinbeta 3 years ago
Workaround:
I find it easier than some webpack setup or using yet another package manager (yapm? yanc? yalc? wut?)
If your React app is bundled with Webpack, you can use a Webpack alias to force all React references to resolve to a single module. In your
webpack.config.js:(Solution from https://github.com/webpack/webpack/issues/8607#issuecomment-453068938)
Is there a workaround for this? How can people locally test react components they want to turn into libraries? :\
Workaround is https://github.com/whitecolor/yalc
It’s not expected that it would work unless you link
reactback from your module to your app.This has actually always been the case (React apps are subtly broken when there are two copies of React module). Hooks surface this immediately which I guess is good.
We do have another issue tracking a better error message for this case.
Thanks @JerryGreen!
For future explorers, the above solution didn’t happen to work in my case. Not sure if it had to do with the use of NextJS, but that is possible!
A slight tweak worked, though: making the package link to my project’s
reactandreact-dom.If you are in the same situation, perhaps try:
This did the trick for us!
Those who still struggling with the issue and work with react-scripts without eject do the following to solve the issue:
yarn add @craco/cracostartscriptFrom:
To
craco.config.jsfile to your root application with the following content:React is great for components but it looks like everything around react is preventing you from creating components. Configuring bundlers and npm dependencies to work with external components is really a hell. And with CRA this is almost impossible. I beg you, please find a solution to this ! Maybe from webpack 5 module federation ?
I configured this in webpack 5 and it’s working for me:
It works! Tks!
Most often, you cannot do this during development though, as you may need React for unit testing or 3rd party tools like Storybook. In a mono-repo, even if React is a peer-dependency of your package, it will fetch the “closest” version, so from your monorepo main
node_modulesinstead of those from the app. I’ve tested Yalc, with some success at the moment, though I am still not sure of the precise differences with a “normal” npm install. This is how we intend to solve this for good in the future, taking inspiration from Meteor solution for this issue, based on anNPM_PACKAGE_DIRSenvironment variable: https://github.com/VulcanJS/vulcan-next/issues/104Nothing from the above was needed! I got mine fixed by just keeping react and react-dom only as peerDependencies in package.json and delete all other occurrences. After that delete node_modules and package-lock.json and install again. Then in should work.
@jaguardo in my experience Yalc removed a ton of the complexity that you would otherwise have to manage with symlinks yourself. Have been using it for a year or so now and the quality is very high. Worth giving it a shot for your use case.
I still get this error message even though I linked react and react-dom (via
yarn link) and set react and react-dom as alias in mywebpack.config:Will this be addressed somehow? I guess there is an workaround - but when you use create react app you can’t really do any hacks in the webpack.config.js 😄
If you’re having this issue when developing a package that exports a React component, and you have a folder structure like this:
Assuming react-component-package has React as a dev and peer dependency, you can run this command from the react-component-package folder:
npm link ../web-application/node_modules/reactThe same command should work with Yarn:
yarn link ../web-application/node_modules/reacthttps://stackoverflow.com/a/58612244
I had the same problem in a Next.js app and solved the issue with the approach suggested by dcecile. I added this entry in
next.config.js:Here are the docs for modifying Next.js webpack configuration.
Hey @dcecile thanks for a quick answer, sadly, that doesn’t work for me as well.
yalc is the only solution to this problem (and the myriad of other issues caused by yarn/npm link) that consistently works.
Its pretty troubling that both yarn and npm seem convinced that naively symlinking entire directories ever worked.
Nothing worked but yalc for me
Currently there are like 3 ways you can install local dependencies,
npm pack on the library repository and yarn add .tgz file in the other repo seems to work for me. The other two give me the hook error.
perfect.
Thanks @Blkc
I had problems using this workaround with React 18. The error was:
To make it work I had to downgrade to a previous React version.
Here the steps I followed:
package.json, and change the versions of bothreactandreact-domto:index.jsto:yarn install(I also deleted thenode_modulesfolder before this, but it should not be necessary)I hope this can help in case someone ends up having the same issue.
Yes. Works like a charm, currently.
But someday, when you
rm -rf node_modulesor move the folder,you will find, wow, something is screwed like a ****!
In YOUR_PROJECT’s package.json add into a jest config section:
It worked for me
For those coming to the issue via a monorepo such as lerna, you can solve the problem by hoisting the dependencies to the root level.
https://github.com/facebook/react/issues/15097
Btw we fixed this issue with the steps here: https://robkendal.co.uk/blog/2019-12-22-solving-react-hooks-invalid-hook-call-warning