-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathApp.js
49 lines (48 loc) · 1.28 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import React from "react";
import { StyleSheet, Text, View, Button } from "react-native";
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
isLoading: true,
_ipAddress: "Unknown"
};
this._getIPAddressUsingAwait = this._getIPAddressUsingAwait.bind(this);
}
_getIPAddressUsingPromise = () => {
return fetch("https://httpbin.org/ip")
.then(response => response.json())
.then(responseJson => {
this.setState({ _ipAddress: responseJson.origin });
})
.catch(error => {
console.error(error);
});
};
async _getIPAddressUsingAwait() {
const response = await fetch("https://httpbin.org/ip");
const json = await response.json();
const data = await json.origin;
this.setState({ _ipAddress: data });
}
render() {
return (
<View style={styles.container}>
<Text>Your current IP address is:</Text>
<Text>{this.state._ipAddress}</Text>
<Button
title="Get IP Address"
onPress={this._getIPAddressUsingAwait.bind(this)}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center"
}
});