[英]How to get React Native packager ip from JavaScript?
有没有办法动态地使用端口或完整路径通过JavaScript代码获取React Native packager ip- jsCodeLocation
? 它可以是localhost:8081
或192.168.0.1.xip.io:8081
。 这取决于运行设备 - 模拟器或真实设备
我在ReactNative源代码中找到了NativeModules并在这里发现了scriptURL:
import { NativeModules } from 'react-native';
...
const scriptURL = NativeModules.SourceCode.scriptURL;
const address = scriptURL.split('://')[1].split('/')[0];
const hostname = address.split(':')[0];
const port = address.split(':')[1];
它适用于模拟器和设备!
import { NativeModules } from 'react-native';
import url from 'url';
const { hostname } = url.parse(NativeModules.SourceCode.scriptURL);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.