I've recently converted an internal component library I'm working on to Styled Components. That component library has Styled Components listed as a peerDep and a devDep in the package.json. From there I'm importing Styled Comopnents into every component that needs styling. All works great up to that point.
I have a Create React App (CRA) application that also imports Styled Comopnents as a regular dependency. I need this to be able to build one-off components for this specific project. BUT I also need the ability to import my component library to build out core components for this new CRA-based app.
Now this is my problem: I'm testing my recently converted Styled Components-based component library in this CRA app by linking directly to the component library in package.json (file:../component-lib). I've installed all my deps, imported components from the component library, built a new project specific Styled Component within the CRA project, and ran it locally only to see this same error: "It looks like there are several instances of 'styled-components' initialized in this application. This may cause dynamic styles not rendering properly, errors happening during rehydration process and makes your application bigger without a good reason." I've read that section of docs and learned not to use npm link and to serve SC as a peerDep and a devDep in my component library.
I believe this issue is not allowing me to access theme props I'm passing into a custom ThemeProvider from the component library since I'm running multiple instances in the CRA project?
This problem is definitely due to my lack of knowledge in dependency management. I'm just wondering if anyone else has encountered a similar issue or what I should be doing to avoid duplicate instances of Styled Comopnents?
Component library index
export { default as Button } from "./components/Button";
export {
default as CustomThemeProvider
} from "./components/utils/CustomThemeProvider";
Component library package.json
"scripts": {
"build": "nwb build-react-component --copy-files",
"clean": "nwb clean-module && npm clean-demo",
"start": "nwb serve-react-demo",
"lint": "eslint src/**",
"test": "nwb test-react",
"styleguide": "styleguidist server",
"styleguide:build": "styleguidist build",
"test:coverage": "nwb test-react --coverage",
"test:watch": "nwb test-react --server",
"publish": "npm run build && npm publish"
},
"dependencies": {
"@rebass/grid": "^6.0.0-4",
"prop-types": "^15.6.0",
"react-portal": "^4.1.2"
},
"peerDependencies": {
"react": "16.x",
"styled-components": "^4.0.3"
},
"devDependencies": {
"babel-eslint": "^8.2.2",
"eslint": "^4.18.2",
"eslint-plugin-react": "^7.7.0"
"prettier": "1.14.3",
"nwb": "0.22.x",
"react": "^16.4.0",
"react-dom": "^16.4.0",
"react-styleguidist": "^7.2.0",
"styled-components": "^4.0.3"
},
Component library NWB config
module.exports = {
type: "react-component",
npm: {
esModules: false,
umd: false,
},
babel: {
stage: 1
}
};
CRA Project package.json
"dependencies": {
"component-library": "0.16.6",
"react": "^16.6.0",
"react-dom": "^16.6.0",
"react-scripts": "2.1.0",
"styled-components": "^4.0.3"
},