React Native Android Fetch failing on connection to local API

You are not able to access your local development server because that port hasn’t been forwarded by ADB yet. When you run react-native run-android, React Native maps the port 8081 with your mobile device on USB. When you disconnect your USB you won’t be able to refresh or hot reload your code anymore. So in this situation you can do 2 things, either map your local server port just like React Native does or use your local IP address.

  1. Mapping Port

    This only works if you are using Android 6.0+. To forward a port using ADB run the following command in your terminal:

    adb reverse tcp:8163 tcp:8163
    

    This will map your local 8163 port to mobile’s 8163 port. You’ll be able to access your development server this way.

  2. Using local IP address

    You can also use your local IP on React Native development app to reload them without USB. Shake your device or long press the menu button to open developer menu. Open Dev Settings, then tap Debug server host & port for device. Here you can enter your machine’s local IP with port number 8081. For ex. if your machine’s IP is 192.168.1.100 then you’d enter 192.168.1.100:8081 in here for successful connection. Now we have covered that we can reload the app. After this when you want to use your local machine’s development server use the same IP with your server’s port number.

You should be good to go with this.

Leave a Comment