How can I get the device's ip on React Native?
There's a ReactNative library for getting information about the devices network:
react-native-network-info
// require module
var NetworkInfo = require('react-native-network-info');
// Get Local IP
NetworkInfo.getIPAddress(ip => {
console.log(ip);
});
import publicIP from 'react-native-public-ip';
publicIP()
.then(ip => {
console.log(ip);
// '47.122.71.234'
})
.catch(error => {
console.log(error);
// 'Unable to get IP address.'
});
For more info: https://www.npmjs.com/package/react-native-public-ip
This package gives following apis
npm i react-native-network-info
import { NetworkInfo } from "react-native-network-info";
// Get Local IP
NetworkInfo.getIPAddress().then(ipAddress => {
console.log(ipAddress);
});
// Get IPv4 IP (priority: WiFi first, cellular second)
NetworkInfo.getIPV4Address().then(ipv4Address => {
console.log(ipv4Address);
});
// Get Broadcast
NetworkInfo.getBroadcast().then(broadcast => {
console.log(broadcast);
});
// Get SSID
NetworkInfo.getSSID().then(ssid => {
console.log(ssid);
});
// Get BSSID
NetworkInfo.getBSSID().then(bssid => {
console.log(bssid);
});
// Get Subnet
NetworkInfo.getSubnet().then(subnet => {
console.log(subnet);
});
// Get Default Gateway IP
NetworkInfo.getGatewayIPAddress().then(defaultGateway => {
console.log(defaultGateway);
});
// Get frequency (supported only for Android)
NetworkInfo.getFrequency().then(frequency => {
console.log(frequency);
});