Skip to content

Instantly share code, notes, and snippets.

@hpneo
Created October 14, 2017 17:44
Show Gist options
  • Save hpneo/d5067103dd103e370dfaf29fd66df390 to your computer and use it in GitHub Desktop.
Save hpneo/d5067103dd103e370dfaf29fd66df390 to your computer and use it in GitHub Desktop.
import React from 'react';
export default class FetchData extends React.Component {
constructor() {
super();
this.state = {
data: null,
isLoading: false,
error: '',
};
}
componentDidMount() {
this.setState({ isLoading: true });
fetch('https://httpbin.org/anything')
.then(response => response.json())
.then((data) => this.setState({
isLoading: false,
data: data,
}))
.catch(error => {
console.log(error);
this.setState({
isLoading: false,
error: error.toString()
});
});
}
render() {
if (this.state.isLoading) {
return <div>Cargando...</div>;
}
if (this.state.error) {
return <div>{this.state.error}</div>;
}
console.log(this.state.data);
return <div>{JSON.stringify(this.state.data)}</div>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment