Expo Snack crashes when running my code in Android, but not in Web or iOS

Hi, all,
I am just starting out learning how to code in React Native, and I am planning to create a joystick for one of my future project. Can you help me out on identifying why my code crashes? Here’s what I am working on:

import React, { useRef, useState } from 'react';
import { View, Text, StyleSheet, Animated, PanResponder } from 'react-native';

const MyComponent = () => {
  const [myBorderColor, setBorderColor] = useState('white');
  const [myBorderWidth, setBorderWidth] = useState(10);

  const pan = useRef(new Animated.ValueXY()).current;

  const panResponder = useRef(
    PanResponder.create({
      onStartShouldSetPanResponder: () => true,
      onPanResponderGrant: () => {
        setBorderColor('lightgreen');
        setBorderWidth(20);
      },
      onPanResponderMove: Animated.event([null, { dx: pan.x, dy: pan.y }]),
      onPanResponderRelease: () => {
        Animated.spring(pan, { toValue: { x: 0, y: 0 } }).start();
        setBorderColor('white');
        setBorderWidth(10);
      },
    })
  ).current;

  return (
    <View style={MyStyle.mainView}>
      <View style={MyStyle.steerArea}>
        <Animated.View
          {...panResponder.panHandlers}
          style={[
            {
              height: 100,
              width: 100,
              backgroundColor: 'gray',
              borderWidth: myBorderWidth,
              borderColor: myBorderColor,
              borderRadius: 50,
              top: pan.y,
              left: pan.x,
            },
          ]}
        />
      </View>
    </View>
  );
};

export default MyComponent;

const MyStyle = StyleSheet.create({
  mainView: {
    flex: 1,
    borderWidth: 10,
    borderRadius: 10,
    justifyContent: 'center',
    backgroundColor: 'lightblue',
  },
  steerArea: {
    height: 200,
    width: 200,
    backgroundColor: 'lightgray',
    borderRadius: 100,
    justifyContent: 'center',
    alignItems: 'center',
    left: 50,
  },
});

It was working just fine until I added the part

const [myBorderWidth, setBorderWidth] = useState(10);

I apologize ahead of time if I happen to post my question on the wrong sub. And please pardon any novice mistake I committed. I’d appreciate if you can point them out so avoid them in the future.

I don’t see anything wrong there.

Do you get any error message?

Does it start working again if you remove the border width stuff?

No error at all. It tries to load then just closes on itself. Works just fine if I remove the 2nd useState from my code.

Edit: tested it again and it’s now giving me an error code

Hey @kaiserv

Did you manage to get to the bottom of this issue?

If I copy your code into a snack it works for me.

Hi! Thanks for checking this out. I just moved on and re-created my code. But based on your input, it might be an issue with my computer not having enough resources to run the code as I’m only using a very low-end PC for this project. But thanks anyway and more power to you.

Hello. I am having the same issue with several of my Snacks.
They work fine in web and iOS (both on my device and on expo snack emulator), but they just crash Expo Go app on Android (also both on my device and on expo emulator).

Expo Go gives no error report, just crashes on startup.

Goes without saying that I’ve tried to clean the cache, erase all data, and even reinstall Expo Go on Android.

Here are the links to two of my snacks that are causing trouble.

Can someone help me with this?
Thanks

I found the following in the adb logcat output:

08-10 10:27:58.250 10007 10469 E unknown:ViewManager: Error while updating prop margin
08-10 10:27:58.250 10007 10469 E unknown:ViewManager: java.lang.reflect.InvocationTargetException
08-10 10:27:58.250 10007 10469 E unknown:ViewManager: 	at java.lang.reflect.Method.invoke(Native Method)
08-10 10:27:58.250 10007 10469 E unknown:ViewManager: 	at abi42_0_0.com.facebook.react.uimanager.ViewManagersPropertyCache$PropSetter.updateShadowNodeProp(ViewManagersPropertyCache.java:7)
08-10 10:27:58.250 10007 10469 E unknown:ViewManager: 	at abi42_0_0.com.facebook.react.uimanager.ViewManagerPropertyUpdater$FallbackShadowNodeSetter.setProperty(ViewManagerPropertyUpdater.java:2)
08-10 10:27:58.250 10007 10469 E unknown:ViewManager: 	at abi42_0_0.com.facebook.react.uimanager.ViewManagerPropertyUpdater.updateProps(ViewManagerPropertyUpdater.java:14)
08-10 10:27:58.250 10007 10469 E unknown:ViewManager: 	at abi42_0_0.com.facebook.react.uimanager.ReactShadowNodeImpl.updateProperties(ReactShadowNodeImpl.java:1)
08-10 10:27:58.250 10007 10469 E unknown:ViewManager: 	at abi42_0_0.com.facebook.react.uimanager.UIImplementation.createView(UIImplementation.java:11)
08-10 10:27:58.250 10007 10469 E unknown:ViewManager: 	at abi42_0_0.com.facebook.react.uimanager.UIManagerModule.createView(UIManagerModule.java:5)
08-10 10:27:58.250 10007 10469 E unknown:ViewManager: 	at java.lang.reflect.Method.invoke(Native Method)
08-10 10:27:58.250 10007 10469 E unknown:ViewManager: 	at abi42_0_0.com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:18)
08-10 10:27:58.250 10007 10469 E unknown:ViewManager: 	at abi42_0_0.com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:2)
08-10 10:27:58.250 10007 10469 E unknown:ViewManager: 	at abi42_0_0.com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
08-10 10:27:58.250 10007 10469 E unknown:ViewManager: 	at android.os.Handler.handleCallback(Handler.java:873)
08-10 10:27:58.250 10007 10469 E unknown:ViewManager: 	at android.os.Handler.dispatchMessage(Handler.java:99)
08-10 10:27:58.250 10007 10469 E unknown:ViewManager: 	at abi42_0_0.com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:1)
08-10 10:27:58.250 10007 10469 E unknown:ViewManager: 	at android.os.Looper.loop(Looper.java:214)
08-10 10:27:58.250 10007 10469 E unknown:ViewManager: 	at abi42_0_0.com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:8)
08-10 10:27:58.250 10007 10469 E unknown:ViewManager: 	at java.lang.Thread.run(Thread.java:764)
08-10 10:27:58.250 10007 10469 E unknown:ViewManager: Caused by: java.lang.IllegalArgumentException: Unknown value: 1em
08-10 10:27:58.250 10007 10469 E unknown:ViewManager: 	at abi42_0_0.com.facebook.react.uimanager.LayoutShadowNode$MutableYogaValue.setFromDynamic(LayoutShadowNode.java:12)
08-10 10:27:58.250 10007 10469 E unknown:ViewManager: 	at abi42_0_0.com.facebook.react.uimanager.LayoutShadowNode.setMargins(LayoutShadowNode.java:4)
08-10 10:27:58.250 10007 10469 E unknown:ViewManager: 	... 17 more

In particular:

08-10 10:27:58.250 10007 10469 E unknown:ViewManager: Error while updating prop margin
[...]
08-10 10:27:58.250 10007 10469 E unknown:ViewManager: Caused by: java.lang.IllegalArgumentException: Unknown value: 1em

After changing the paragraph margin to an integer it works on Android.

Thank you

You are a life saver.

Now that we (I) know what is the cause, should it be reported as bug? It seems strange that this works on iOS and not Android, and at least this should be caught by Expo Go on Android and reported to user on error screen.

As far as I know it’s a known React Native issue and I think they might be unwilling to fix it to avoid negatively affecting performance. But I might be wrong. See what you can find in the Issues in the React Native repository on GitHub.

At least you know for next time :slight_smile:
adb logcat can be useful, although there’s a lot of junk to wade through.

1 Like

Hi,
Could you please explain how to go about this solution in detail?
I am having the same issue here:

It’s easier to debug locally, so I downloaded the ZIP file, unzipped it, ran yarn in the unzipped directory, ran expo start and opened it in Expo Go. At that point I got the following printed in the terminal and a similar error in Expo Go:

Logs for your project will appear below. Press Ctrl+C to exit.
Android Bundling complete 14998ms
Android Running app on SM-G950F

Invariant Violation: Invalid prop `resizeMode` of value `` supplied to `StyleSheet cardImage`, expected one of ["center","contain","cover","repeat","stretch"].
StyleSheet cardImage: {
  "width": null,
  "backgroundColor": "red",
  "alignSelf": "center",
  "alignItems": "center",
  "borderRadius": 20,
  "marginBottom": 20,
  "resizeMode": ""
}
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:104:6 in reportException
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:172:19 in handleException
at node_modules/react-native/Libraries/Core/setUpErrorHandling.js:24:6 in handleError
at node_modules/@react-native/polyfills/error-guard.js:49:36 in ErrorUtils.reportFatalError
at node_modules/metro-runtime/src/polyfills/require.js:204:6 in guardedLoadModule
at http://192.168.1.2:19000/node_modules/expo/AppEntry.bundle?platform=android&dev=true&hot=false&minify=false:118795:3 in global code

Invariant Violation: "main" has not been registered. This can happen if:
* Metro (the local dev server) is run from the wrong folder. Check if Metro is running, stop it and restart it in the current project.
* A module failed to load due to an error and `AppRegistry.registerComponent` wasn't called.
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:104:6 in reportException
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:172:19 in handleException
at node_modules/react-native/Libraries/Core/setUpErrorHandling.js:24:6 in handleError
at node_modules/@react-native/polyfills/error-guard.js:49:36 in ErrorUtils.reportFatalError

After removing “resizeMode” it just crashes Expo Go.

Then I deleted all of the contents of the StyleSheet.create(...) call:

const styles = StyleSheet.create({
  cardImage: {
  },
  card: {
  },
  swipablecard: {
  },
  profileStatus: {
  },
});

At that point it no longer crashes.

If I put back only the contents of the cardImage styles, Expo Go doesn’t crash, but there are several errors.

const styles = StyleSheet.create({
  cardImage: {
    width: win - 50,
    backgroundColor: 'red',
    alignSelf: 'center',
    alignItems: 'center',
    borderRadius: 20,
    marginBottom: 20,
  },
  card: {
  },
  swipablecard: {
  },
  profileStatus: {
  },
});

If I put back just the card or swipablecard styles there are no errors or crashes.

const styles = StyleSheet.create({
  cardImage: {
  },
  card: {
    flex: 1,
    flexGrow: 1,
    borderRadius: 10,
    borderWidth: 2,
    borderColor: '#E8E8E8',
    justifyContent: 'center',
    backgroundColor: 'white',
  },
  swipablecard: {
  },
  profileStatus: {
  },
});

or:

const styles = StyleSheet.create({
  cardImage: {
  },
  card: {
  },
  swipablecard: {
    width: '98%',
    height: 600,
    overflow: 'hidden',
    alignSelf: 'center',
    alignItems: 'center',
    borderRadius: 20,
    marginBottom: 20,
  },
  profileStatus: {
  },
});

If I put back just the profileStatus styles Expo Go crashes immediately.

const styles = StyleSheet.create({
  cardImage: {
  },
  card: {
  },
  swipablecard: {
  },
  profileStatus: {
    height: 'fitContent',
    paddingTop: 20,
    paddingBottom: 20,
    paddingLeft: 5,
    paddingRight: 5,
    backgroundColor: 'grey',
    borderRadius: 10,
    marginBottom: 20,
  },
});

So it seems that the problems are in the profileStatus and cardImage styles.

If I put back just the card and swipablecard styles without the cardImage and profileStatus styles then there is still no crash/error.

const styles = StyleSheet.create({
  cardImage: {
  },
  card: {
    flex: 1,
    flexGrow: 1,
    borderRadius: 10,
    borderWidth: 2,
    borderColor: '#E8E8E8',
    justifyContent: 'center',
    backgroundColor: 'white',
  },
  swipablecard: {
    width: '98%',
    height: 600,
    overflow: 'hidden',
    alignSelf: 'center',
    alignItems: 'center',
    borderRadius: 20,
    marginBottom: 20,
  },
  profileStatus: {
  },
});

The most suspicious-looking part of the profileStatus styles is this line:

    height: 'fitContent',

If I remove the cardImage styles and remove the height style from profileStatus then again there’s no crash and no error.

OK, so let’s look at cardImage next. The most suspicious looking part of that is this line:

    width: win - 50,

where win is defined as follows:

const win = Dimensions.get('screen');

If I get rid of the width line from the cardImage styles, again there are no crashes or errors. If you log the contents of win you’ll see it’s not a number, so win - 50 doesn’t make sense:

const win = Dimensions.get('screen');
console.log(JSON.stringify(win));
{"width":360,"height":740,"scale":3,"fontScale":1}

So you need to use win.width rather than just win, like this:

// [...]
const win = Dimensions.get('screen');

const styles = StyleSheet.create({
  cardImage: {
    width: win.width - 50,
    backgroundColor: 'red',
    alignSelf: 'center',
    alignItems: 'center',
    borderRadius: 20,
    marginBottom: 20,
  },
  card: {
    flex: 1,
    flexGrow: 1,
    borderRadius: 10,
    borderWidth: 2,
    borderColor: '#E8E8E8',
    justifyContent: 'center',
    backgroundColor: 'white',
  },
  swipablecard: {
    width: '98%',
    height: 600,
    overflow: 'hidden',
    alignSelf: 'center',
    alignItems: 'center',
    borderRadius: 20,
    marginBottom: 20,
  },
  profileStatus: {
    paddingTop: 20,
    paddingBottom: 20,
    paddingLeft: 5,
    paddingRight: 5,
    backgroundColor: 'grey',
    borderRadius: 10,
    marginBottom: 20,
  },
});
// [...]

Wow! Wow!! Wow!!! Thank you so very much for taking your time to be this detailed. I appreciate you. I wish I can follow you on Twitter, Github, or elsewhere.(Looking for a mentor too). You are awesome!. Thank you

Glad I could help

1 Like