2020/01/30
import React, { Component } from 'react';
import axios from 'axios';
class Example extends Component {
getGeoInfo = () => {
axios
.get('https://ipapi.co/json/')
.then(response => {
let data = response.data;
this.setState({
countryName: data.country_name,
countryCode: data.country_calling_code,
});
if (response.data) {
localStorage.setItem('countryCode', data.country_calling_code);
}
})
.catch(error => {
console.log('dddd', error);
});
};
componentDidMount() {
this.getGeoInfo();
}
constructor(props) {
super(props);
this.state = {
countryName: '',
countryCode: '',
};
}
render() {
return (
<div>
<p>Country Name: {this.state.countryName}</p>
<p>Country Code: {this.state.countryCode}</p>
</div>
);
}
}
export default Example;