question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hooks don't work with yarn link

See original GitHub issue

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:

  1. Create a library that’s built with hooks (my-hooks-lib)
  2. Create a local app that uses the library (my-react-app) using CRA
  3. 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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:34
  • Comments:36 (1 by maintainers)

github_iconTop GitHub Comments

302reactions
jerrygreencommented, Mar 5, 2020

Workaround:

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?)

72reactions
dcecilecommented, 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:

{
    /* ... */
    module: {
        rules: [ /* ... */ ],
    },
    resolve: {
        alias: { react: require.resolve("react") }
    },
}

(Solution from https://github.com/webpack/webpack/issues/8607#issuecomment-453068938)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Yarn link: invalid hook call | Wiki
Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of...
Read more >
yarn workspaces and lerna cause Invalid Hook call
I'm trying to prove that React Hooks don't work properly in container . – Liu Lei. Mar 20 at 0:30. Add a comment ......
Read more >
Unsolving the mysteries of yarn/npm link for libraries ...
The easy way to fix it is by using the link command, we just go to our library and we create a link...
Read more >
FAQs - styled-components
How can I fix issues when using npm link or yarn link? Local linking can be a useful tool to co-develop projects simultaneously....
Read more >
Troubleshooting | React Navigation
If that doesn't work, you can also try the following: ... If you use yarn : ... If you don't use state persistence...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found