Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the device's ip on React Native?

In node we would use require('os') but we cannot do such thing on react native. Any ideas?

like image 440
jsdario Avatar asked Jul 26 '16 16:07

jsdario


2 Answers

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);
});
like image 93
Chris Avatar answered Oct 19 '22 02:10

Chris


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

like image 43
Nexus242 Avatar answered Oct 19 '22 02:10

Nexus242