styled-components: Conflict with Webpack's 5 Federated Modules (Invalid hook call)
When trying to consume a federated module that uses styled-components, React throws the “Invalid hook call” error.
Environment
System
- OS: macOS Mojave 10.14.6
- CPU: (12) x64 Intel® Core™ i7-8750H CPU @ 2.20GHz
- Memory: 1.13 GB / 16.00 GB
- Shell: 3.2.57 - /bin/bash
Binaries
- Node: 12.16.1 - /usr/local/bin/node
- npm: 6.13.4 - /usr/local/bin/npm
Dependencies
- react@16.13.1
- styled-components@5.2.0
Reproduction
See this repository for reproduction code:
It contains 2 npm projects. Do npm install on both and start them in order (first foo then shell). Once everything is running, if you navigate to /foo you should see that the app crashes.
Steps to reproduce
- Create an
npmproject which exposes a component that usesstyled-components. - Create another
npmproject that consumes and renders the federated module.
Expected Behavior
React should be able to render the styled component.
Actual Behavior
React throws a Invalid hook call error on the console when trying to render the styled component.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 17
Hey Guys, I know it’s more Webpack than styled components related, but this seems the only issue I found on the Internet so far about the invalid hook call which I’m struggling with.
I’ve got 2 webpack bundles, first is for the components library, the second is the host, the app that uses it.
Components library:
And the host app:
When I try to use the
Panelcomponent in my host like that:I get the same
invalid hookerror. I think there might be somehow 2 React instances loading but after comparing the configurations posted above, I didn’t see the difference. 😞I’m seeing this issue too. I really would like to use module federation. I’m on
“react”: “16.13.1”, “react-dom”: “16.13.1”,
Using styled components is ok for me, but when I use ant design, it crashes with
You probably need to share react, or some context dependency of styled components. Make sure that these things are registered as singletons. If you post a repo somewhere I can take a look. Pretty sure we’ve seen this one before already at webpack.
Check all the relevant repos, hosts and remote are sharing react and react dom, singletons.
Make sure they share styled components. If you’re using dynamic imports, that’s best. If this is a static import it’s sub-optimal.
What it sounds like is either another copy of react is being pulled across, or there’s two separately theme or context providers being created.
You need to remove scripts from html if you have them. Also make sure to share react dom as well. Don’t want multiple react instances loading.
If you don’t share it though then remotes will download their own copy. Trust me you want to share it if it’s installed on anything else. If it’s not. Then you’re good