question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[React-Native] Expo Web doesn't support `styled.Component` notation

See original GitHub issue

Environment

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

  1. Create a new project with Expo, using TypeScript.
  2. Add styled-components as dependency.
  3. The target "web" should be specified in app.json, like so:
"platforms": [
  "ios",
  "android",
  "web"
],

Steps to reproduce

  1. Add the following line to any used file: import styled from 'styled-components/native.
  2. 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:closed
  • Created 4 years ago
  • Reactions:9
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
geelencommented, Jun 26, 2019

Oooh so I was able to confirm that this is fixed in v5 but only if you import styled from 'styled-components/native' (not import styled from 'styled-components')

image image

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:

image image

Switching to styled(Text) “works”, but treats the Text component like a web component, passing className to it, which ends up with wonky styles:

image

(Lol, just figured out that that was because I had line-height: 24; not line-height: 24px;. But that shows you about how the DOM and RN versions of SC differ, hey?)

You also get this warning:

image

Which is discussed in this RNW issue: https://github.com/necolas/react-native-web/issues/1146

As for styled-components/native vs styled-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 injecting className 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)

6reactions
geelencommented, Jun 27, 2019

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:

// config-overrides.js from react-app-rewired

module.exports = function(config, env) {
  config.resolve = {
    ...config.resolve,
    alias: {
      ...config.resolve.alias,
      'react-native': path.resolve(__dirname, 'mocks/react-native')
    }
  };

  return config;
};
// mocks/react-native.js
import * as RN from 'react-native-web';
export default RN;

export {
  createElement,
  findNodeHandle,
  render,
  unmountComponentAtNode, // modules
  processColor,
  NativeModules,
  TextPropTypes,
  ViewPropTypes, // APIs
  AccessibilityInfo,
  Alert,
  Animated,
  AppRegistry,
  AppState,
  AsyncStorage,
  BackHandler,
  Clipboard,
  DeviceInfo,
  Dimensions,
  Easing,
  I18nManager,
  InteractionManager,
  Keyboard,
  LayoutAnimation,
  Linking,
  NativeEventEmitter,
  NetInfo,
  PanResponder,
  PixelRatio,
  Platform,
  Share,
  StyleSheet,
  UIManager,
  Vibration, // components
  ActivityIndicator,
  Button,
  CheckBox,
  FlatList,
  Image,
  ImageBackground,
  KeyboardAvoidingView,
  ListView,
  Modal,
  Picker,
  ProgressBar,
  RefreshControl,
  SafeAreaView,
  ScrollView,
  SectionList,
  Slider,
  StatusBar,
  SwipeableFlatList,
  SwipeableListView,
  Switch,
  Text,
  TextInput,
  Touchable,
  TouchableHighlight,
  TouchableNativeFeedback,
  TouchableOpacity,
  TouchableWithoutFeedback,
  View,
  VirtualizedList,
  YellowBox, // propTypes
  ColorPropType,
  EdgeInsetsPropType,
  PointPropType, // compat (components)
  DatePickerIOS,
  DrawerLayoutAndroid,
  ImageEditor,
  ImageStore,
  InputAccessoryView,
  MaskedViewIOS,
  NavigatorIOS,
  PickerIOS,
  ProgressBarAndroid,
  ProgressViewIOS,
  SegmentedControlIOS,
  SnapshotViewIOS,
  TabBarIOS,
  ToastAndroid,
  ToolbarAndroid,
  ViewPagerAndroid,
  WebView, // compat (apis)
  ActionSheetIOS,
  AlertIOS,
  CameraRoll,
  DatePickerAndroid,
  ImagePickerIOS,
  PermissionsAndroid,
  PushNotificationIOS,
  Settings,
  StatusBarIOS,
  Systrace,
  TimePickerAndroid,
  TVEventHandler,
  VibrationIOS
} from 'react-native-web';

Ain’t pretty, but it works. Gets me styled-components v4.3.2 working with react-native-web v0.11.4

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found