Cannot create styled-component for component [object Object]
See original GitHub issueEnvironment
npx envinfo --system --binaries --npmPackages styled-components,babel-plugin-styled-components --markdown --clipboard
## System:
- OS: macOS High Sierra 10.13.6
- CPU: (4) x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
- Memory: 160.71 MB / 16.00 GB
- Shell: 5.3 - /bin/zsh
## Binaries:
- Node: 8.9.4 - ~/.nvm/versions/node/v8.9.4/bin/node
- Yarn: 1.10.1 - ~/.nvm/versions/node/v8.9.4/bin/yarn
- npm: 6.4.1 - ~/.nvm/versions/node/v8.9.4/bin/npm
## npmPackages:
- styled-components: ^4.1.1 => 4.1.1
Reproduction
As I was trying to upgrade our application from react native 0.55.0 to 0.57.5, I am running into issues with styled-components. Specifically, when I start the app in Android emulator, I get
Cannot create styled-component for component [object Object]
(Surprisingly, I do not get such errors in iOS emulator!)
The relevant portion of Typography.js (as shows in the image above):
const BaseTypography = styled.Text`
color: ${getFontColor};
${color}
${props => props.capitalize && capitalizeFirstLetter};
${space};
`
I am using React 16.6.1, styled-components 4.1.1, @babel/core 7.0.0
Any ideas? I’ve seen https://github.com/styled-components/styled-components/issues/1308 , but I am not sure if it’s relevant.
Steps to reproduce
Expected Behavior
No error
Actual Behavior
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:20 (5 by maintainers)
Top Results From Across the Web
Uncaught Error: Cannot create styled-component for ... - GitHub
I'm getting this type of error with a very simple component: Uncaught Error: Cannot create styled-component for component: undefined at ...
Read more >Jest - `Cannot create styled-component for ... - Stack Overflow
Show activity on this post. I'm running into the following error when testing my application using jest : FAIL ● Test suite failed...
Read more >cannot create styled-component for component: undefined.
I get the Cannot create styled-component for component: undefined error when I try to instantiate the ServerStyleSheet but it works fine without it....
Read more >Unhandled JS Exception: Cannot create styled-component for ...
[Solved]-Unhandled JS Exception: Cannot create styled-component for component: [object Object]-Reactjs ... At some point I introduced what I can only guess were ...
Read more >FAQs - styled-components
By declaring a styled component inside the render method of a react component, you are dynamically creating a new component on every render....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
So, here are what we did, and it somehow worked:
I removed
babel-polyfill
in all places (but had to add it to one other file)We were importing styled-components in different places in two different ways. In the docs for styled-components, we found this about using the metro bundler for installation. https://www.styled-components.com/docs/basics#simpler-usage-with-the-metro-bundler
This seems to only be happening on Debug Android builds, NOT release/production builds.
Here is an example of how we are creating a styled component.
Now lets look at how React Native views are represented…
For debug builds, this returns
View == ', '{"$$typeof":60112}'
//object representing a forward ref For release builds, this returnsView == ', 'RCTView'
//stringWhich will configure the styled components for each React Native view here…
If you take a look at the
react-is
variables, you will see it will initialize the following const…This will get set to
{ _k: 'Symbol(react.forward_ref)_k.i0kqk9t5s8' }
for both release and debug builds.Now lets look at the
react-is
isValidElementType() function, which is called from hereFor release builds, it will return early after hitting this line, because the
tag
param isRCTView
But for debug builds, it will end up checking the Symbol references, which are NOT set correctly in debug builds.
ex…
If I hack react-is to not use the symbols, and simply use the hex values (the defaults), then it works on debug builds.
I am not sure what the correct solution is here… I hope this puts someone on the right track to fixing it 😃