因此,我需要获取用户的网络IPV4地址。这是我的以下代码和我用来获取IPV4地址的库。它们都不起作用。
import React, { useState } from 'react'
import { Text, View, Button } from 'react-native'
import axios from 'axios';
import * as Network from 'expo-network';
import NetInfo from "@react-native-community/netinfo";
import { NetworkInfo } from "react-native-network-info";
import DeviceInfo from 'react-native-device-info';
import publicIP from 'react-native-public-ip';
const App = () => {
const [ipAddress, setIPAddress] = useState('');
//react-native-network-info
NetworkInfo.getIPV4Address().then(ipv4Address => {
console.log(ipv4Address);
setIPAddress(ipv4Address);
});
//react-native-device-info
const getIpAddress = async () => {
const ip = await DeviceInfo.getIpAddress();
console.log(ip);
setIPAddress(ip);
};
//react-native-public-ip
publicIP()
.then(ip => {
console.log(ip);
setIPAddress(ip);
});
//@react-native-community/netinfo
NetInfo.fetch().then(state => {
console.log("Connection type", state.type);
console.log("Is connected?", state.isConnected);
});
//axios
const getData = async () => {
const res = await axios.get('https://geolocation-db.com/json/')
console.log(res.data);
setIPAddress(res.data.IPv4)
};
//expo-network
const ipAlert = async () => {
const ip = await Network.getIpAddressAsync()
setIPAddress(ip);
};
return (
<View style={{ top: 200 }}>
<Text style={{ textAlign: 'center', fontSize: 20 }}>{ipAddress}</Text>
<Button title='GET IP' onPress={getIpAddress} />
</View>
)
};
export default App;
正如你所看到的,我的代码已经尝试了所有的可能性。当我运行代码时,我要么得到公共IP地址,要么得到错误
错误:不变量冲突:无法调用JavaScript模块方法AppRegistry.runApplication()。模块尚未注册为可调用。已注册的可调用JavaScript模块(n = 11):Systrace、JSTimers、HeapCapture、SamplingProfiler、RCTLog、RCTDeviceEventEmitter、RCTNativeAppEventEmitter、GlobalPerformanceLogger、JSDevSupportModule、HMRClient、RCTEventEmitter。错误的常见原因是应用程序项目档案路径不正确。当JS配套损毁或载入React Native时发生早期初始化错误时,也会发生这种情况。
如有任何帮助,不胜感激。
:)
顺便说一句,我分别试了每一个,而不是全部一起。
1条答案
按热度按时间htrmnn0y1#
我发现您的错误中存在AppRegistry问题。
因此,您必须检查根index.js文件,并检查App.js的导入路径(在以下示例中突出显示),并替换app.js的实际路径。
例如:
**从'./App'导入App;**检查此路径
/
封装在我的程式码中运作。