最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to get React Native packager ip from JavaScript? - Stack Overflow

programmeradmin1浏览0评论

Is there any way to get dynamicly React Native packager ip with port or full path to bundle - jsCodeLocation through JavaScript code? It can be localhost:8081 or 192.168.0.1.xip.io:8081. It depends on the running device - simulator or real device

Is there any way to get dynamicly React Native packager ip with port or full path to bundle - jsCodeLocation through JavaScript code? It can be localhost:8081 or 192.168.0.1.xip.io:8081. It depends on the running device - simulator or real device

Share Improve this question edited Oct 7, 2016 at 7:13 artem_russkikh asked Oct 7, 2016 at 7:00 artem_russkikhartem_russkikh 4374 silver badges15 bronze badges 2
  • For now, I found it here in javascript console __fbBatchedBridgeConfig.remoteModuleConfig[2][1].scriptURL – artem_russkikh Commented Oct 7, 2016 at 7:12
  • This working only in remote chrome debugging – artem_russkikh Commented Oct 7, 2016 at 8:18
Add a comment  | 

3 Answers 3

Reset to default 15

I found NativeModules in ReactNative source code and discover scriptURL here:

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];

It works on simulator and device!

import { NativeModules } from 'react-native';
import url from 'url';

const { hostname } = url.parse(NativeModules.SourceCode.scriptURL);

2024 Updated version on 0.73 - TurboModules - New Architecture enabled

const SourceCode = TurboModuleRegistry.getEnforcing("SourceCode");
const scriptURL = SourceCode.getConstants().scriptURL;
const address = scriptURL.split('://')[1].split('/')[0];
const hostname = address.split(':')[0];
const port = address.split(':')[1];
发布评论

评论列表(0)

  1. 暂无评论