You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 link in my-hooks-lib and in my-react-app run yarn 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
Reacted by Kyle Thomson, Daniel Little, Marcell Toth, clodal, Oboo Cheng, Safwan Shaheer, Jerry Green, Travis Fischer, Josh Wilson, Vadorequest and 30 more
It's not expected that it would work unless you link react back 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.
Reacted by quisi.do, Doniyor Aliyev, André Matulionis dos Santos, shent, Bruno Lemos, Nathaniel Tucker, Mikolaj Burawski, Scott Dickerson, Yvonnick FRIN, Dan Cecile and 28 more
Reacted by Chris Nelson
Reacted by Dmitry Gonchar, Artem Shubovych, Zach, Jon Marsh, Lucien Perouze, Brandon Pittman, Ryan Zaki, Christopher Danan and Anna Tsukanova
Reacted by Aadam Ishmael and Irfan Andriansyah
Reacted by Ender Bonnet, Constantine Antonakos, Levente Balogh, Jerry Green, Karolis Krušinskas, Jacano, beingPranjal and Aadam Ishmael
Reacted by Ose Diaz-Gonzalez, 范晓, Stiliyana Yordanova, Paul Li, Camilo, dpikt, Blake Mealey, Josejulio Martínez, David Dostal, Safwan Shaheer and 31 more
Reacted by Sam Richard, Michael Hamilton, Martin Kapal and Leian Ivey
Reacted by Esteban Piñones Valenzuela, James, Rodrigo Amaral, Ivan Kleshnin, Naeim Taheri and Joe Green
Reacted by Gonzalo Chumillas, Lukas Novotny, Lewis James-Odwin, MisterChateau, Jaume Tarradas Llort, dannypk, Naeim Taheri and Joe Green
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:
cd PACKAGE_YOU_DEBUG_LOCALLY
yarn link
yarn install
cd node_modules/react
yarn link
cd ../../node_modules/react-dom
yarn link
cd YOUR_PROJECT
yarn link PACKAGE_YOU_DEBUG_LOCALLY
yarn link react
yarn link react-dom
I find it easier than some webpack setup or using yet another package manager (yapm? yanc? yalc? wut?)
Reacted by Matt Wolszczak, Henrique Limas, Yannick Cornaille, Renato Böhler, Mateusz Gawliński, André Cortés, Jihad D. Waspada, Abidh Muhammed, Maggie Yu, Justin Lee and 174 more
Reacted by Michael Rodov, Ivan Kleshnin, Yanay Hollander, zopelee, Remi Sture, Defne and Aleksand Dev
Reacted by Sehi L'Yi, Victor Magarlamov, Julien Rioux, Remi Sture, Damien Erambert and Anastacio Menezes
Reacted by Yannick Cornaille, André Cortés, Jihad D. Waspada, Abidh Muhammed, Maggie Yu, David Janda, Luis Perez Bautista, Lee Taylor, Aibol Kussain, José Breijo and 46 more
Reacted by Chanuka Asanka, Jan Dockal, Wallace Batista, fragno, Erenndriel, Gavin Gilmour, Carlos Henrique Rabelo, Rose Wiegley, Matthieu Caron, Kevin King and 41 more
Reacted by amadeirado, ♪人来疯, Rodrigo Amaral, Indra Gunawan, Juan Ruben Eliett, Arvin-zhu, Stephen Estrada, bowen-wu, Kenny, Prabjot Singh and 19 more
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/react
The same command should work with Yarn: yarn link ../web-application/node_modules/react
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 😄
Reacted by Wynn Slater, Safwan Shaheer, Michael Rodov, Max Donchenko and Juan Rebella
cd PACKAGE_YOU_DEBUG_LOCALLY
yarn link
yarn install
cd node_modules/react
yarn link
cd ../../node_modules/react-dom
yarn link
cd YOUR_PROJECT
yarn link PACKAGE_YOU_DEBUG_LOCALLY
yarn link react
yarn link react-dom
I find it easier than some webpack setup or using yet another package manager (yapm? yanc? yalc? wut?)
cd PACKAGE_YOU_DEBUG_LOCALLY
yarn link
yarn install
cd node_modules/react
yarn link
cd ../../node_modules/react-dom
yarn link
cd YOUR_PROJECT
yarn link PACKAGE_YOU_DEBUG_LOCALLY
yarn link react
yarn link react-dom
I find it easier than some webpack setup or using yet another package manager (yapm? yanc? yalc? wut?)
@jerrygreen thanks dude. You saved us. Works like a charm!
cd PACKAGE_YOU_DEBUG_LOCALLY
yarn link
yarn install
cd node_modules/react
yarn link
cd ../../node_modules/react-dom
yarn link
cd YOUR_PROJECT
yarn link PACKAGE_YOU_DEBUG_LOCALLY
yarn link react
yarn link react-dom
I find it easier than some webpack setup or using yet another package manager (yapm? yanc? yalc? wut?)
@jerrygreen thanks dude. You saved us. Works like a charm!
Yes. Works like a charm, currently.
But someday, when you rm -rf node_modules or move the folder,
you will find, wow, something is screwed like a ****!
cd YOUR_PROJECT
cd node_modules/react
yarn link
cd ../react-dom
yarn link
cd PACKAGE_YOU_DEBUG_LOCALLY
yarn link
yarn install
yarn link react
yarn link react-dom
cd YOUR_PROJECT
yarn link PACKAGE_YOU_DEBUG_LOCALLY
I had problems using this workaround with React 18. The error was:
Module not found: Error: Can't resolve 'react-dom/client'
To make it work I had to downgrade to a previous React version.
Here the steps I followed:
go to package.json, and change the versions of both react and react-dom to:
"react": "^17.0.2",
"react-dom": "^17.0.2",
change index.js to:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();
yarn install (I also deleted the node_modules folder before this, but it should not be necessary)
then follow the steps of the original workaround
I hope this can help in case someone ends up having the same issue.
I created a small command line tool that solved this issue for me by watching the build folder of a project when developing locally and installing it in the node_modules of a destination project: https://github.com/valdemarrolfsen/herd.js.
Its a quite simple tool but it worked perfectly for me at least :)
based on @Omrilu-ma solution, i got the following error, "craco Module not found: Error: You attempted to import /node_modules/react which falls outside of the project src/ directory."
I Solved this problem with the following additional configuration in Craco:
Activity
gaearon commentedon Nov 19, 2018
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.
andy9775 commentedon Nov 22, 2018
My mistake - first time using
yarn linkwith react. Thanks for addressing @gaearoncorysimmons commentedon Jun 28, 2019
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
dcecile commentedon Jul 5, 2019
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 webpack/webpack#8607 (comment))
maciekgrzybek commentedon Jul 30, 2019
@dcecile just to clarify, do you resolve that in the library you're working? or in the demo app?
dcecile commentedon Jul 30, 2019
@maciekgrzybek I resolve it in the demo app
maciekgrzybek commentedon Jul 30, 2019
Hey @dcecile thanks for a quick answer, sadly, that doesn't work for me as well.
LucidityDesign commentedon Dec 19, 2019
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:blkc commentedon Feb 19, 2020
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.
jerrygreen commentedon Mar 5, 2020
Workaround:
I find it easier than some webpack setup or using yet another package manager (yapm? yanc? yalc? wut?)
kas commentedon May 27, 2020
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
danantal commentedon Jun 3, 2020
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 😄
brennick commentedon Sep 4, 2020
Btw we fixed this issue with the steps here: https://robkendal.co.uk/blog/2019-12-22-solving-react-hooks-invalid-hook-call-warning
jsmapr1 commentedon Sep 29, 2020
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.
#15097
10 remaining items
Omrilu-ma commentedon May 27, 2021
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:alex-ds commentedon Jul 2, 2021
In YOUR_PROJECT's package.json add into a jest config section:
It worked for me
ramyNiranjan commentedon Aug 8, 2021
ramyNiranjan commentedon Aug 8, 2021
This solution worked for me, Thanks a lot
jquintozamora commentedon Sep 22, 2021
I configured this in webpack 5 and it's working for me:
added yalc due to react/react#14257
muratx10 commentedon Apr 13, 2022
@jerrygreen thanks dude. You saved us. Works like a charm!
zopelee commentedon Apr 14, 2022
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 ****!
DaviDevMod commentedon Apr 20, 2022
What about a
postinstallscript that creates the links? So that once you deletenode_modulesand runnpm installyou get the links back?DaviDevMod commentedon Apr 20, 2022
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.
sickdyd commentedon Jun 6, 2022
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.
jakebanks commentedon Jun 23, 2022
Yalc worked for me also, seems like a great tool
valdemarrolfsen commentedon Jul 29, 2022
I created a small command line tool that solved this issue for me by watching the build folder of a project when developing locally and installing it in the node_modules of a destination project: https://github.com/valdemarrolfsen/herd.js.
Its a quite simple tool but it worked perfectly for me at least :)
harpreetkhalsagtbit commentedon Apr 26, 2023
perfect.
Thanks @blkc
Sassar88 commentedon Jul 19, 2023
based on @Omrilu-ma solution, i got the following error, "craco Module not found: Error: You attempted to import /node_modules/react which falls outside of the project src/ directory."
I Solved this problem with the following additional configuration in Craco: