Several instances of 'styled-components' initialized in CRA micro-frontend application

Viewed 3801

I'm running a micro frontend application with multiple React versions, each micro frontend repo is using lazy loading for dynamically loading its React version and it works as expected (Yay!) The micro frontend app is structured as follows: enter image description here

That being said, I am experiencing issues with the styled-components multi-versioning, and receiving the following console warning in dev mode (not in production):

enter image description here

For more technical details check out the example repo with the implementation.

Since the application is using CRA to simplify the configurations of webpack/babel I was wondering if there is a good way to improve the initial configuration to resolve this console warning.

I checked the docs link shared on the warning, and while I understand micro frontend wouldn't be the best way to maintain your project, we'd still like to provide this option to our users as they might need to gradually migrate their project versions, so I'm still interested in getting this console warning resolved.

Any tips or suggested solutions would be appreciated.

printed warning:

react_devtools_backend.js:2430 It looks like there are several instances of 'styled-components' initialized in this application. This may cause dynamic styles to not render properly, errors during the rehydration process, a missing theme prop, and makes your application bigger without good reason. See https://s-c.sh/2BAXzed for more info.

2 Answers

I was facing the same issue and I solved it by moving all my styled components inside remote apps and exposed them from there.

I had the same issue with my micro-service application. For each micro-service I had package.json file and one global package.json for all micro-service. So, in the global package.json I added resolution, it allowed me to have only one version of styled component for my app

"resolutions": { "styled-components": "4.2.1" },

Also, I used to lerna. It can help you with your micro-service's dependencies.

I hope my answer can help you resolve your problem.

Related