Home > Mobile >  Rendering IP address with react-native-public-ip in <Text>
Rendering IP address with react-native-public-ip in <Text>

Time:06-22

I'm struggling to render publicIP client-side inside of a Text block; I'm including import publicIP from 'react-native-public-ip'; and have tried the following:

<Text>{publicIP()}</Text>
<Text>{ip()}</Text>

But nothing seems to take.

I'm able to render it with the example linked in their README, but not simply render as text. Any examples I find from Googling around use console.log as examples. What am I missing here?

CodePudding user response:

publicIP() is asynchronous function

here is how you can make it:

[IP, setIP] = useState("");

useEffect(() => {
 publicIP()
  .then(ip => {
    setIP(ip);
  })
  .catch(error => {
    setIP("error")
  });
},[])

return (
  <View>
    <Text>{IP}</Text>
  </View>
)

  • Related