Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native Expo localhost request not work on android emulator

I simply use "fetch" to send GET request to my localhost API but it shows network fail. It works to send GET request to other non localhost website.

I would like to know the way to access my localhost API. the environment is that I use NativeBase and run it by Expo on android emulator. and the Expo connection is LAN.

Thank you.

like image 948
fmchan Avatar asked Oct 22 '25 06:10

fmchan


2 Answers

you must use the ip instead of localhost:

for windows: ipconfig (IP ADDRESS) for mac: networksetup -getinfo Wi-Fi (IP ADDRESS)

the request would be like that:

axios.get('http://192.168.0.34')

I recommend using axios for react native

like image 169
Diego Santa Cruz Mendezú Avatar answered Oct 24 '25 07:10

Diego Santa Cruz Mendezú


You can get an IP address at runtime using expo-constants module's manifest.

If you did not install this module, Run expo install expo-constants

import Constants from 'expo-constants';
const { manifest } = Constants;
...
const uri = `http://${manifest.debuggerHost
  .split(`:`)
  .shift()
  .concat(`:1000`)}`; // Switch to the port you use. ex)localhost:3000 => .concat(`:3000`)}
like image 34
hong developer Avatar answered Oct 24 '25 06:10

hong developer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!