[React-Native] Expo Web doesn't support `styled.Component` notation
See original GitHub issueEnvironment
System:
- OS: macOS 10.15
- CPU: (8) x64 Intel® Core™ i7-7820HQ CPU @ 2.90GHz
- Memory: 1.31 GB / 16.00 GB
- Shell: 5.7.1 - /bin/zsh
Binaries:
- Node: 10.15.3 - ~/.nvm/versions/node/v10.15.3/bin/node
- Yarn: 1.15.2 - /usr/local/bin/yarn
- npm: 6.4.1 - ~/.nvm/versions/node/v10.15.3/bin/npm
- Watchman: 4.9.0 - /usr/local/bin/watchman
npmPackages:
- styled-components: ^4.3.1 => 4.3.1
Reproduction
- Create a new project with Expo, using TypeScript.
- Add
styled-components
as dependency. - The target
"web"
should be specified inapp.json
, like so:
"platforms": [
"ios",
"android",
"web"
],
Steps to reproduce
- Add the following line to any used file:
import styled from 'styled-components/native
. - Use
const Test = styled.View
somewhere.
Expected Behavior
Module builds, everything compiles and I get my styled component.
Actual Behavior
Module build warns (does not error for some reason):
Attempted import error: 'react-native' does not contain a default export (imported as 'reactNative').
in
styled-components/native/dist/styled-components.native.esm.js
It works fine if I import the component separately and use styled()
as a function, like so:
import styled from 'styled-components';
import { View } from 'react-native';
const Test = styled(View) //...
Issue Analytics
- State:
- Created 4 years ago
- Reactions:9
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Using Styled Components - Expo Documentation
A guide for using Styled Components with Expo. Styled Components is a CSS-in-JS solution that enables you to create React components with a...
Read more >Could not find a declaration file for module 'styled-components ...
If you add styled-components to your React Native project, there's a sub-directory for the native components specifically:
Read more >Why Is styled-components "styled" Wrapper Not Working With ...
When applying styling to an existing (own or third party) React component using styled-components' styled(MyComponent) notation, ...
Read more >How to use styled-components with React Native
In this tutorial, we'll discuss what advantages a library like styled-components has over the general StyleSheet manager in React Native.
Read more >React Fundamentals - React Native
You can think of components as blueprints. Whatever a function component returns is rendered as a React element. React elements let you describe ......
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
Oooh so I was able to confirm that this is fixed in
v5
but only if youimport styled from 'styled-components/native'
(notimport styled from 'styled-components'
)Same behaviour with
styled(Text)
with the same import.With
import styled from 'styled-components'
, on Expo web you get the DOM version, which is still busted in the same way:Switching to
styled(Text)
“works”, but treats theText
component like a web component, passing className to it, which ends up with wonky styles:(Lol, just figured out that that was because I had
line-height: 24;
notline-height: 24px;
. But that shows you about how the DOM and RN versions of SC differ, hey?)You also get this warning:
Which is discussed in this RNW issue: https://github.com/necolas/react-native-web/issues/1146
As for
styled-components/native
vsstyled-components
, I couldn’t remember when that changed. Turns out it was part of the v3 release 18 months ago. I’m guessing anyone who’s done the RNW conversion until this point has been accidentally injectingclassName
props and things have been mostly ok, until now.TL;DR: v5 works as long as you import
styled-components/native
!(Edit: posted my code at https://github.com/geelen/expo-demo-project if anyone needs it)
Found another way, since something we were doing in our app was conflicting with the hooks implementation of v5.
You can stub out the default export in RNW like this:
Ain’t pretty, but it works. Gets me styled-components v4.3.2 working with react-native-web v0.11.4