You can get the IP address at runtime using the Expo manifest:
import Constants from "expo-constants";
const { manifest } = Constants;
const api = (typeof manifest.packagerOpts === `object`) && manifest.packagerOpts.dev
? manifest.debuggerHost.split(`:`).shift().concat(`:3000`)
: `api.example.com`;
This will set api
constant to the address of your local development machine in development mode and to whatever address you use in production. Note that apps deployed through App Store / Play Store seems to have packagerOpts undefined
. That’s why we have additional typeof
condition. In that case we assume it’s production build.
More about the manifest here: https://docs.expo.io/versions/latest/workflow/how-expo-works/#expo-development-server