-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Invalid hook call error when I use my own library from styled-components #3187
Comments
Give [email protected] a try. |
@zantinger Thanks! I was experiencing the exact same issue--created a ui library with react and styled-components and attempted to load a really basic button into a clean create-react-app. It works fine if I don't use styled-components in the exported library button but as soon as I make it a styled button I get that error. I tried all the trouble-shooting in the link included with the error and confirmed all my versions matched, I correctly used devDeps and peerDeps, and I'm not using any hooks at all (so far just have really minimal repos for proof-of-concept). Changing styled components to 4.3.2 made it work! But... after contemplating why I wouldn't be able to use the latest version of styled-components in a brand new library that's using the latest react and a brand new create-react-app, I remembered that I had set |
So we can't easily help everyone with this but this is always a problem with duplicate
The first case is often easily checked using |
After running in the next error i extend my webpack config like this. Now styled components looks like to work in version 5 |
I had the same issue. But I had installed styled-components in my home directory instead of in the front-end which is where my react lives, after moving it the issue was resolved & I'm still using styled components 5.
|
Removing react and react-dom in the component library indeed solved the problem, thanks! However, removing them makes the library far less practical. I used to preset components from other libraries like Material-ui, or re-write entirely new components, but I now have to choose between having a highly-customized components library, or using styled-components to style html elements and components I can't customize, which is a bit sad! For instance I can't use expressions like this anymore, upon which I relied heavily in my own library:
Do you think there would be any solution to keep the best of both worlds, and export elaborate components like these using a library? Thanks in advance! Edit: I simply needed to move react and react-dom from "dependencies" to "peerDependencies" in package.json and now it works like a charm! 👍 |
If anyone else is seeing this and moving out react and react-dom to peerDeps isn't doing it for you, move This guy tipped me off: https://styled-components.com/docs/faqs#i-am-a-library-author-should-i-bundle-styledcomponents-with-my-library |
This ultimately ended up working for me (after scrapping my project setup and basically just copying/stripping https://github.com/React95/React95), in that I was using On that note though, any ideas on how to actually be able to use |
|
it works for me as well, thanks |
it works for me as well. thanks |
Failed: Tried to link
Worked Link the
Note:
Stackoverflow: https://stackoverflow.com/a/77781022/2980414 |
I created my own UI tool from styled-components library (using typescript and rollup).
For example, I have the following component:
So, I use this component in another project:
And, when I run the application, I get:
The above error occurred in the <styled.div> component:
in styled.div (at ...)
...
Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
See ... react-invalid-hook-call for tips about how to debug and fix this problem.
I think there is problem with styled-components because when I use a simple without styled-components, my component is displayed on the application.
Here my github repository: https://github.com/aehp20/ionic-react-my-ui-components
Thank you in advance!
The text was updated successfully, but these errors were encountered: