そのスレッドの答えの1つに基づいて、このようなネイティブ反作用で実装することができます -
import React, { Component } from 'react';
import { AppRegistry, StyleSheet, Text, View } from 'react-native';
export default class Test extends Component {
constructor(props) {
super(props);
this.state = {
countryName: '',
regionName: ''
};
}
componentDidMount() {
var url = 'https://freegeoip.net/json/';
fetch(url)
.then((response) => response.json())
.then((responseJson) => {
//console.log(responseJson);
this.setState({
countryName: responseJson.country_name,
regionName: responseJson.region_name
});
})
.catch((error) => {
//console.error(error);
});
}
render() {
return (
<View style={styles.container}>
<Text>Country: {this.state.countryName}</Text>
<Text>Region: {this.state.regionName}</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
});
AppRegistry.registerComponent('Test',() => Test);
ありがとうございました。 012(応答)=> response.json()を説明してください。 .then((responseJson)=> {'? –
' response.json() 'は応答のJSONデータを返します。 'それで'約束。詳細はこちらhttps://developer.mozilla.org/en-US/docs/Web/API/Body/json – vinayr
ありがとうございました:) –