Welcome aboard! We are happy you are here and wish you good net-raft!
// this is the complete code, copy and use
import React from 'react';
const API = 'https://geoip-db.com/json';
const DEFAULT_QUERY = 'redux';
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
hits: [],
isLoading: false,
};
this.setStateHandler = this.setStateHandler.bind(this);
}
setStateHandler() {
this.setState({ isLoading: true });
fetch(API + DEFAULT_QUERY)
.then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error('Something went wrong ...');
}
})
.then(data => this.setState({ hits: data.IPv4, isLoading: false }))
.catch(error => this.setState({ error, isLoading: false }));
}
render() {
return (
<div>
<button onClick = {this.setStateHandler}>Click</button>
<h2>{this.state.hits}</h2>
</div>
);
}
}
export default App;
// npm install node-fetch --save
import React from 'react';
import fetch from 'node-fetch';
class App extends React.Component {
constructor() {
super();
this.state = {
data: []
}
this.setStateHandler = this.setStateHandler.bind(this);
};
setStateHandler() {
fetch('https://geoip-db.com/json')
.then(res => res.json())
.then(json => this.setState({data: json.IPv4}))
};
render() {
return (
<div>
<button onClick = {this.setStateHandler}>Click</button>
<h2>{this.state.data}</h2>
</div>
);
}
}
export default App;
import React from 'react';
import fetch from 'node-fetch';
export default class App extends React.Component {
constructor(props) {
super(props);
}
componentDidMount() {
fetch('http://freegeoip.net/json/')
.then(res => res.json())
.then(json => {
this.setState({text: json.ip});
});
}
render() {
return(
<div>
{this.state.text}
</div>
);
}
}
The most helpful REACT solutions
How to get client ip address using react ?REACT
Click to see more ...
19.6K
421
how to get the type of a device using react ?REACT
Click to see more ...
2.4K
220
create digital clock reactREACT
Click to see more ...
1.8K
141
country dropdown using reactREACT
Click to see more ...
1.4K
127
create alarm clock using reactREACT
Click to see more ...
5.4K
115
How to get latitude and longitude using react/javascript ?REACT
Click to see more ...
1.2K
108
get client country reactREACT
Click to see more ...
934
82
get ip address reactREACT
Click to see more ...
1.2K
81
time ago reactREACT
Click to see more ...
746
74
detect mobile device reactREACT
Click to see more ...
710
58