-
Notifications
You must be signed in to change notification settings - Fork 299
Error when using hooks #99
Comments
I also encountered this problem, I am looking for a problem. 😤 |
Interesting, can you share a repo with an example? |
I made a quick repro repo if that helps: https://github.com/taylorham/hooks-issue |
I've found that the issue seems to come from using a However, now you've got to commit, push (or publish), and reinstall the package every time you want to view a change, which adds a LOT of friction to the process. Building with your own I'm not sure if this issue is with npm/yarn and how they're reading local dependencies, or if it's to do with It's definitely not a problem with Does anyone have a clue which tree I should start barking up? I feel like the React devs should be alerted since it's makes developing Hooks-based libraries a real slog. |
Just found this today after a little more digging. So the React team is aware of it, and it occurs because of npm/yarn's resolution of Dan has made a few comments addressing this, but it seems it's relatively expected and unlikely to change in the near future. A good fix for now is to make your example depend on your library's version of React by changing the example's "react": "link:../node_modules/react" Hope this helps people who are itching to use Hooks ASAP! |
Try to remove caret in your "react": "16.7.0-alpha.2",
"react-dom": "16.7.0-alpha.2", It worked for me: facebook/react#14454 (comment) |
Neither @taylorham's nor @doasync's suggestion worked for me 😕 |
@levrik, you probably have two different versions of react |
@doasync nope. I checked my bundle. It only includes one version of React. The issue is caused by another library which implements a custom renderer. |
I'm also experiencing this issue, tried installing the exact same version but still get the same hook call error. |
@antoniocapelo what I did was to remove react and react dom from After that, it worked great. |
I found that @taylorham 's worked, but it also needed the same change made for
Also FWIW, my parent
Works fine. |
Sorry, running into this now, Just curious when you say
Is this in peerDependencies? I am trying to figure out how to get this issue resolved. |
@bwighthunter read it again, the change gets made in Confirming the answer provided by @nickmcmillan |
Seems like you also have to update ( This PR seems good: https://github.com/transitive-bullshit/create-react-library/pull/134/files |
I had to explicitly run |
I'm a beginner to this, so I don't know how to actually integrate my solution into create-react-library (yet). I've updated all the babel and react packages, and posted it to my github. It doesn't have the cool walkthrough of package creation that |
This is a problem caused by 2 different versions of React being used: 1 for the React component you're trying to bundle up, 1 for the React example you're using. One of them loads first, and when your hooky component gets loaded, another React fires up and is like "Whoa! You can't do hook stuff right here!" The best way I've found around this is to completely give up on It's not perfectly convenient as it's not a symlink and you have to reinstall your local pkg every time you want to test it, but at least you can get around this hook/link seemingly unsolvable problem. |
@corysimmons I LOVE YOU, yalc was the only thing that worked for me. |
Forgot to update my comments on this kind of stuff, but yalc can be config'd to just auto-update the pkg you're working on using nodemon (or any cli watcher). Add Now every time you change anything in your local lib, it will update that pkg anywhere it has been installed (locally) automatically. I love you too @vagyriag 💖 |
Unfortunately this doesn’t appear to trigger a hot reload in the example app - but at least the hooks errors are gone. |
I know this might not be the answer you're looking for, but according to React docs, this is not an error, but a feature by design. You can find the reference here: https://reactjs.org/docs/hooks-overview.html |
I believe this should now be fixed via #151 and the other major changes made in the latest Please let me know if this is not the case. |
Not sure why this answer does not have any positive reactions. I was having the same error, I added hook in a regular JS function instead of React function. Thanks, man! 🎉 |
I'm bootstrapping a React component library with this tool and I get:
Hooks can only be called inside the body of a function component
.Steps to reproduce:
create-react-library
.react
andreact-dom
tov16.7.0-alpha.0
in./package.json
and./src/package.json
.src/
.If you try the example you get the error. One weird thing is that if you use hooks in the
example
you don't get any error.The text was updated successfully, but these errors were encountered: