Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-native android debugger connecting to 10.0.2.2 instead of localhost

While running app with react-native run-android its connecting to 10.0.2.2:8081 instead of localhost:8081 and not able to debug.

Does anyone know how to fix so that it will connect to localhost instead?

like image 233
sKhan Avatar asked Mar 26 '18 19:03

sKhan


People also ask

How do I connect React Native app to debugger?

Connecting a React Native app to React Native Debugger To connect your app with React Native Debugger, you need to run your app and start debug mode. To start debug mode, shake your mobile device or press Command + Shift + Z or Ctrl + M and choose the debug option. We're all set to begin using React Native Debugger.

How do I connect React Native to local server?

Running your React Native applicationInstall the Expo Go app on your iOS or Android phone and connect to the same wireless network as your computer. On Android, use the Expo Go app to scan the QR code from your terminal to open your project. On iOS, use the built-in QR code scanner of the default iOS Camera app.

How do I get localhost API in React Native?

In Android, we can use the IP 10.0. 2.2 to access computers localhost. In a project, we can use the above logic to decide what baseUrl to use based on the platform. Afterwards, we can use this baseUrl for all APIs.


2 Answers

On MAC I solved it by doing following:

  1. Press Cmd + M on emulator screen
  2. Dev settings > Debug server host & port for device
  3. Set localhost:8081
  4. Rerun the android app: react-native run-android

Debugger is connected now!

Hope it will help others :)

like image 87
Anshuman Jaiswal Avatar answered Sep 21 '22 01:09

Anshuman Jaiswal


Just run port forwarding

adb -s emulator-5554 reverse tcp:8081 tcp:8081

OR your api server to port 5000

adb -s emulator-5554 reverse tcp:5000 tcp:5000
like image 24
Антон Васильев Avatar answered Sep 19 '22 01:09

Антон Васильев